Whamcloud - gitweb
8270675c1744b14a04c02b30ed27434b6a149744
[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         "xiafs",
78         NULL
79 };
80
81 #ifdef DEV_DSK_DEVICES
82 static const char *base_devices[] = {
83         "/dev/dsk/hda",
84         "/dev/dsk/hdb",
85         "/dev/dsk/hdc",
86         "/dev/dsk/hdd",
87         "/dev/dsk/hd1a",
88         "/dev/dsk/hd1b",
89         "/dev/dsk/hd1c",
90         "/dev/dsk/hd1d",
91         "/dev/dsk/sda",
92         "/dev/dsk/sdb",
93         "/dev/dsk/sdc",
94         "/dev/dsk/sdd",
95         "/dev/dsk/sde",
96         "/dev/dsk/sdf",
97         "/dev/dsk/sdg",
98         NULL
99 };
100 #else
101 static const char *base_devices[] = {
102         "/dev/hda",
103         "/dev/hdb",
104         "/dev/hdc",
105         "/dev/hdd",
106         "/dev/hd1a",
107         "/dev/hd1b",
108         "/dev/hd1c",
109         "/dev/hd1d",
110         "/dev/sda",
111         "/dev/sdb",
112         "/dev/sdc",
113         "/dev/sdd",
114         "/dev/sde",
115         "/dev/sdf",
116         "/dev/sdg",
117         NULL
118 };
119 #endif
120
121 /*
122  * Global variables for options
123  */
124 char *devices[MAX_DEVICES];
125 char *args[MAX_ARGS];
126 int num_devices, num_args;
127
128 int verbose = 0;
129 int doall = 0;
130 int noexecute = 0;
131 int serialize = 0;
132 int skip_root = 0;
133 int like_mount = 0;
134 int notitle = 0;
135 int parallel_root = 0;
136 int progress = 0;
137 int force_all_parallel = 0;
138 char *progname;
139 char *fstype = NULL;
140 struct fs_info *filesys_info;
141 struct fsck_instance *instance_list;
142 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
143 char *fsck_path = 0;
144 static int ignore(struct fs_info *);
145
146 static char *string_copy(const char *s)
147 {
148         char    *ret;
149
150         ret = malloc(strlen(s)+1);
151         if (ret)
152                 strcpy(ret, s);
153         return ret;
154 }
155
156 static char *skip_over_blank(char *cp)
157 {
158         while (*cp && isspace(*cp))
159                 cp++;
160         return cp;
161 }
162
163 static char *skip_over_word(char *cp)
164 {
165         while (*cp && !isspace(*cp))
166                 cp++;
167         return cp;
168 }
169
170 static void strip_line(char *line)
171 {
172         char    *p;
173
174         while (*line) {
175                 p = line + strlen(line) - 1;
176                 if ((*p == '\n') || (*p == '\r'))
177                         *p = 0;
178                 else
179                         break;
180         }
181 }
182
183 static char *parse_word(char **buf)
184 {
185         char *word, *next;
186
187         word = *buf;
188         if (*word == 0)
189                 return 0;
190
191         word = skip_over_blank(word);
192         next = skip_over_word(word);
193         if (*next)
194                 *next++ = 0;
195         *buf = next;
196         return word;
197 }
198
199 static void free_instance(struct fsck_instance *i)
200 {
201         if (i->prog)
202                 free(i->prog);
203         if (i->device)
204                 free(i->device);
205         free(i);
206         return;
207 }
208
209 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
210 {
211         char    *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
212         struct fs_info *fs;
213
214         *ret_fs = 0;
215         strip_line(line);
216         if ((cp = strchr(line, '#')))
217                 *cp = 0;        /* Ignore everything after the comment char */
218         cp = line;
219
220         device = parse_word(&cp);
221         mntpnt = parse_word(&cp);
222         type = parse_word(&cp);
223         opts = parse_word(&cp);
224         freq = parse_word(&cp);
225         passno = parse_word(&cp);
226
227         if (!device)
228                 return 0;       /* Allow blank lines */
229         
230         if (!mntpnt || !type)
231                 return -1;
232         
233         if (!(fs = malloc(sizeof(struct fs_info))))
234                 return -1;
235
236         fs->device = string_copy(device);
237         fs->mountpt = string_copy(mntpnt);
238         fs->type = string_copy(type);
239         fs->opts = string_copy(opts ? opts : "");
240         fs->freq = freq ? atoi(freq) : -1;
241         fs->passno = passno ? atoi(passno) : -1;
242         fs->flags = 0;
243         fs->next = NULL;
244
245         *ret_fs = fs;
246
247         return 0;
248 }
249
250 /*
251  * Interpret the device name if necessary 
252  */
253 static char *interpret_device(char *spec)
254 {
255         char *dev = NULL;
256         
257         if (!strncmp(spec, "UUID=", 5))
258                 dev = get_spec_by_uuid(spec+5);
259         else if (!strncmp(spec, "LABEL=", 6))
260                 dev = get_spec_by_volume_label(spec+6);
261         else
262                 return spec;
263         if (dev) {
264                 free(spec);
265                 return (dev);
266         }
267         /*
268          * Check to see if this was because /proc/partitions isn't
269          * found.
270          */
271         if (access("/proc/partitions", R_OK) < 0) {
272                 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
273                         strerror(errno));
274                 fprintf(stderr, "Is /proc mounted?\n");
275                 exit(1);
276         }
277         /*
278          * Check to see if this is because we're not running as root
279          */
280         if (geteuid())
281                 fprintf(stderr, "Must be root to scan for matching "
282                         "filesystems: %s\n", spec);
283         else
284                 fprintf(stderr, "Couldn't find matching filesystem: %s\n", 
285                         spec);
286         exit(1);
287 }
288
289 /*
290  * Load the filesystem database from /etc/fstab
291  */
292 static void load_fs_info(const char *filename)
293 {
294         FILE    *f;
295         char    buf[1024];
296         int     lineno = 0;
297         int     old_fstab = 1;
298         struct fs_info *fs, *fs_last = NULL;
299
300         filesys_info = NULL;
301         if ((f = fopen(filename, "r")) == NULL) {
302                 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
303                         filename, strerror(errno));
304                 return;
305         }
306         while (!feof(f)) {
307                 lineno++;
308                 if (!fgets(buf, sizeof(buf), f))
309                         break;
310                 buf[sizeof(buf)-1] = 0;
311                 if (parse_fstab_line(buf, &fs) < 0) {
312                         fprintf(stderr, _("WARNING: bad format "
313                                 "on line %d of %s\n"), lineno, filename);
314                         continue;
315                 }
316                 if (!fs)
317                         continue;
318                 if (!filesys_info)
319                         filesys_info = fs;
320                 else
321                         fs_last->next = fs;
322                 fs_last = fs;
323                 if (fs->passno < 0)
324                         fs->passno = 0;
325                 else
326                         old_fstab = 0;
327         }
328         
329         fclose(f);
330         
331         if (old_fstab) {
332                 fprintf(stderr, _("\007\007\007"
333                 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
334                 "       field.  I will kludge around things for you, but you\n"
335                 "       should fix your /etc/fstab file as soon as you can.\n\n"));
336                 
337                 for (fs = filesys_info; fs; fs = fs->next) {
338                         fs->passno = 1;
339                 }
340         }
341 }
342         
343 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
344 static struct fs_info *lookup(char *filesys)
345 {
346         struct fs_info *fs;
347         int     try_again = 0;
348
349         /* No filesys name given. */
350         if (filesys == NULL)
351                 return NULL;
352
353         for (fs = filesys_info; fs; fs = fs->next) {
354                 if (strchr(fs->device, '='))
355                         try_again++;
356                 if (!strcmp(filesys, fs->device) ||
357                     !strcmp(filesys, fs->mountpt))
358                         break;
359         }
360
361         if (fs || !try_again)
362                 return fs;
363
364         for (fs = filesys_info; fs; fs = fs->next) {
365                 fs->device = interpret_device(fs->device);
366                 if (!strcmp(filesys, fs->device) ||
367                     !strcmp(filesys, fs->mountpt))
368                         break;
369         }
370
371         return fs;
372 }
373
374 /* Find fsck program for a given fs type. */
375 static char *find_fsck(char *type)
376 {
377   char *s;
378   const char *tpl;
379   static char prog[256];
380   char *p = string_copy(fsck_path);
381   struct stat st;
382
383   /* Are we looking for a program or just a type? */
384   tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
385
386   for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
387         sprintf(prog, tpl, s, type);
388         if (stat(prog, &st) == 0) break;
389   }
390   free(p);
391   return(s ? prog : NULL);
392 }
393
394 static int progress_active(NOARGS)
395 {
396         struct fsck_instance *inst;
397
398         for (inst = instance_list; inst; inst = inst->next) {
399                 if (inst->flags & FLAG_DONE)
400                         continue;
401                 if (inst->flags & FLAG_PROGRESS)
402                         return 1;
403         }
404         return 0;
405 }
406
407 /*
408  * Execute a particular fsck program, and link it into the list of
409  * child processes we are waiting for.
410  */
411 static int execute(const char *type, char *device, char *mntpt,
412                    int interactive)
413 {
414         char *s, *argv[80], prog[80];
415         int  argc, i;
416         struct fsck_instance *inst, *p;
417         pid_t   pid;
418
419         inst = malloc(sizeof(struct fsck_instance));
420         if (!inst)
421                 return ENOMEM;
422         memset(inst, 0, sizeof(struct fsck_instance));
423
424         sprintf(prog, "fsck.%s", type);
425         argv[0] = string_copy(prog);
426         argc = 1;
427         
428         for (i=0; i <num_args; i++)
429                 argv[argc++] = string_copy(args[i]);
430
431         if (progress & !progress_active()) {
432                 if (strcmp(type, "ext2") == 0) {
433                         argv[argc++] = string_copy("-C0");
434                         inst->flags |= FLAG_PROGRESS;
435                 }
436         }
437
438         argv[argc++] = string_copy(device);
439         argv[argc] = 0;
440
441         s = find_fsck(prog);
442         if (s == NULL) {
443                 fprintf(stderr, _("fsck: %s: not found\n"), prog);
444                 return ENOENT;
445         }
446
447         if (verbose || noexecute) {
448                 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
449                 for (i=0; i < argc; i++)
450                         printf("%s ", argv[i]);
451                 printf("\n");
452         }
453         
454         /* Fork and execute the correct program. */
455         if (noexecute)
456                 pid = -1;
457         else if ((pid = fork()) < 0) {
458                 perror("fork");
459                 return errno;
460         } else if (pid == 0) {
461                 if (!interactive)
462                         close(0);
463                 (void) execv(s, argv);
464                 perror(argv[0]);
465                 exit(EXIT_ERROR);
466         }
467
468         for (i=0; i < argc; i++)
469                 free(argv[i]);
470         
471         inst->pid = pid;
472         inst->prog = string_copy(prog);
473         inst->type = string_copy(type);
474         inst->device = string_copy(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  * Wait for one child process to exit; when it does, unlink it from
493  * the list of executing child processes, and return it.
494  */
495 static struct fsck_instance *wait_one(NOARGS)
496 {
497         int     status;
498         int     sig;
499         struct fsck_instance *inst, *inst2, *prev;
500         pid_t   pid;
501
502         if (!instance_list)
503                 return NULL;
504
505         if (noexecute) {
506                 inst = instance_list;
507                 instance_list = inst->next;
508                 inst->exit_status = 0;
509                 return(inst);
510         }
511
512         /*
513          * gcc -Wall fails saving throw against stupidity
514          * (inst and prev are thought to be uninitialized variables)
515          */
516         inst = prev = NULL;
517         
518         do {
519                 pid = wait(&status);
520                 if (pid < 0) {
521                         if ((errno == EINTR) || (errno == EAGAIN))
522                                 continue;
523                         if (errno == ECHILD) {
524                                 fprintf(stderr,
525                                         _("%s: wait: No more child process?!?\n"),
526                                         progname);
527                                 return NULL;
528                         }
529                         perror("wait");
530                         continue;
531                 }
532                 for (prev = 0, inst = instance_list;
533                      inst;
534                      prev = inst, inst = inst->next) {
535                         if (inst->pid == pid)
536                                 break;
537                 }
538         } while (!inst);
539
540         if (WIFEXITED(status)) 
541                 status = WEXITSTATUS(status);
542         else if (WIFSIGNALED(status)) {
543                 sig = WTERMSIG(status);
544                 if (sig == SIGINT) {
545                         status = EXIT_UNCORRECTED;
546                 } else {
547                         printf(_("Warning... %s for device %s exited "
548                                "with signal %d.\n"),
549                                inst->prog, inst->device, sig);
550                         status = EXIT_ERROR;
551                 }
552         } else {
553                 printf(_("%s %s: status is %x, should never happen.\n"),
554                        inst->prog, inst->device, status);
555                 status = EXIT_ERROR;
556         }
557         inst->exit_status = status;
558         if (prev)
559                 prev->next = inst->next;
560         else
561                 instance_list = inst->next;
562         if (progress && (inst->flags & FLAG_PROGRESS) &&
563             !progress_active()) {
564                 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
565                         if (inst2->flags & FLAG_DONE)
566                                 continue;
567                         if (strcmp(inst2->type, "ext2"))
568                                 continue;
569                         /*
570                          * If we've just started the fsck, wait a tiny
571                          * bit before sending the kill, to give it
572                          * time to set up the signal handler
573                          */
574                         if (inst2->start_time < time(0)+2) {
575                                 if (fork() == 0) {
576                                         sleep(1);
577                                         kill(inst2->pid, SIGUSR1);
578                                         exit(0);
579                                 }
580                         } else
581                                 kill(inst2->pid, SIGUSR1);
582                         break;
583                 }
584         }
585         return inst;
586 }
587
588 /*
589  * Wait until all executing child processes have exited; return the
590  * logical OR of all of their exit code values.
591  */
592 static int wait_all(NOARGS)
593 {
594         struct fsck_instance *inst;
595         int     global_status = 0;
596
597         while (instance_list) {
598                 inst = wait_one();
599                 if (!inst)
600                         break;
601                 global_status |= inst->exit_status;
602                 free_instance(inst);
603         }
604         return global_status;
605 }
606
607 /*
608  * Run the fsck program on a particular device
609  * 
610  * If the type is specified using -t, and it isn't prefixed with "no"
611  * (as in "noext2") and only one filesystem type is specified, then
612  * use that type regardless of what is specified in /etc/fstab.
613  * 
614  * If the type isn't specified by the user, then use either the type
615  * specified in /etc/fstab, or DEFAULT_FSTYPE.
616  */
617 static void fsck_device(char *device, int interactive)
618 {
619         const char *type = 0;
620         struct fs_info *fsent;
621         int retval;
622
623         if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
624                 type = fstype;
625
626         if ((fsent = lookup(device))) {
627                 device = fsent->device;
628                 if (!type)
629                         type = fsent->type;
630         }
631         if (!type)
632                 type = DEFAULT_FSTYPE;
633
634         retval = execute(type, device, fsent ? fsent->mountpt : 0,
635                          interactive);
636         if (retval) {
637                 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
638                         "for %s\n"), progname, retval, type, device);
639         }
640 }
641
642 /* See if filesystem type matches the list. */
643 static int fs_match(char *type, char *fs_type)
644 {
645   int ret = 0, negate = 0;
646   char list[128];
647   char *s;
648
649   if (!fs_type) return(1);
650
651   if (strncmp(fs_type, "no", 2) == 0) {
652         fs_type += 2;
653         negate = 1;
654   }
655   strcpy(list, fs_type);
656   s = strtok(list, ",");
657   while(s) {
658         if (strcmp(s, type) == 0) {
659                 ret = 1;
660                 break;
661         }
662         s = strtok(NULL, ",");
663   }
664   return(negate ? !ret : ret);
665 }
666
667
668 /* Check if we should ignore this filesystem. */
669 static int ignore(struct fs_info *fs)
670 {
671         const char **ip;
672         int wanted = 0;
673
674         /*
675          * If the pass number is 0, ignore it.
676          */
677         if (fs->passno == 0)
678                 return 1;
679
680         /*
681          * If a specific fstype is specified, and it doesn't match,
682          * ignore it.
683          */
684         if (!fs_match(fs->type, fstype)) return 1;
685         
686         /* Are we ignoring this type? */
687         for(ip = ignored_types; *ip; ip++)
688                 if (strcmp(fs->type, *ip) == 0) return(1);
689
690         /* Do we really really want to check this fs? */
691         for(ip = really_wanted; *ip; ip++)
692                 if (strcmp(fs->type, *ip) == 0) {
693                         wanted = 1;
694                         break;
695                 }
696
697         /* See if the <fsck.fs> program is available. */
698         if (find_fsck(fs->type) == NULL) {
699                 if (wanted)
700                         fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
701                                 fs->device, fs->type);
702                 return(1);
703         }
704
705         /* We can and want to check this file system type. */
706         return 0;
707 }
708
709 /*
710  * Return the "base device" given a particular device; this is used to
711  * assure that we only fsck one partition on a particular drive at any
712  * one time.  Otherwise, the disk heads will be seeking all over the
713  * place.
714  */
715 static const char *base_device(char *device)
716 {
717         const char **base;
718
719         for (base = base_devices; *base; base++) {
720                 if (!strncmp(*base, device, strlen(*base)))
721                         return *base;
722         }
723         return device;
724 }
725
726 /*
727  * Returns TRUE if a partition on the same disk is already being
728  * checked.
729  */
730 static int device_already_active(char *device)
731 {
732         struct fsck_instance *inst;
733         const char *base = base_device(device);
734
735         if (force_all_parallel)
736                 return 0;
737
738         for (inst = instance_list; inst; inst = inst->next) {
739                 if (!strcmp(base, base_device(inst->device)))
740                         return 1;
741         }
742         return 0;
743 }
744
745 /* Check all file systems, using the /etc/fstab table. */
746 static int check_all(NOARGS)
747 {
748         struct fs_info *fs = NULL;
749         struct fsck_instance *inst;
750         int status = EXIT_OK;
751         int not_done_yet = 1;
752         int passno = 1;
753         int pass_done;
754
755         if (verbose)
756                 printf(_("Checking all file systems.\n"));
757
758         /*
759          * Find and check the root filesystem first.
760          */
761         if (!parallel_root) {
762                 for (fs = filesys_info; fs; fs = fs->next) {
763                         if (!strcmp(fs->mountpt, "/"))
764                                 break;
765                 }
766                 if (fs && !skip_root && !ignore(fs)) {
767                         fs->device = interpret_device(fs->device);
768                         fsck_device(fs->device, 1);
769                         fs->flags |= FLAG_DONE;
770                         status |= wait_all();
771                         if (status > EXIT_NONDESTRUCT)
772                                 return status;
773                 }
774         }
775         if (fs) fs->flags |= FLAG_DONE;
776
777         /*
778          * Mark filesystems that should be ignored as done.
779          */
780         for (fs = filesys_info; fs; fs = fs->next) {
781                 if (ignore(fs))
782                         fs->flags |= FLAG_DONE;
783         }
784                 
785         while (not_done_yet) {
786                 not_done_yet = 0;
787                 pass_done = 1;
788
789                 for (fs = filesys_info; fs; fs = fs->next) {
790                         if (fs->flags & FLAG_DONE)
791                                 continue;
792                         /*
793                          * If the filesystem's pass number is higher
794                          * than the current pass number, then we don't
795                          * do it yet.
796                          */
797                         if (fs->passno > passno) {
798                                 not_done_yet++;
799                                 continue;
800                         }
801                         /*
802                          * If a filesystem on a particular device has
803                          * already been spawned, then we need to defer
804                          * this to another pass.
805                          */
806                         if (device_already_active(fs->device)) {
807                                 pass_done = 0;
808                                 continue;
809                         }
810                         /*
811                          * Spawn off the fsck process
812                          */
813                         fs->device = interpret_device(fs->device);
814                         fsck_device(fs->device, serialize);
815                         fs->flags |= FLAG_DONE;
816
817                         if (serialize) {
818                                 pass_done = 0;
819                                 break; /* Only do one filesystem at a time */
820                         }
821                 }
822                 if (verbose > 1)
823                         printf(_("--waiting-- (pass %d)\n"), passno);
824                 inst = wait_one();
825                 if (inst) {
826                         status |= inst->exit_status;
827                         free_instance(inst);
828                 }
829                 if (pass_done) {
830                         status |= wait_all();
831                         if (verbose > 1) 
832                                 printf("----------------------------------\n");
833                         passno++;
834                 } else
835                         not_done_yet++;
836         }
837         status |= wait_all();
838         return status;
839 }
840
841 static void usage(NOARGS)
842 {
843         fprintf(stderr,
844                 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
845         exit(EXIT_USAGE);
846 }
847
848 static void PRS(int argc, char *argv[])
849 {
850         int     i, j;
851         char    *arg;
852         char    options[128];
853         int     opt = 0;
854         int     opts_for_fsck = 0;
855         
856         num_devices = 0;
857         num_args = 0;
858         instance_list = 0;
859
860         progname = argv[0];
861
862         for (i=1; i < argc; i++) {
863                 arg = argv[i];
864                 if (!arg)
865                         continue;
866                 if ((arg[0] == '/' && !opts_for_fsck) ||
867                     (strncmp(arg, "LABEL=", 6) == 0) ||
868                     (strncmp(arg, "UUID=", 5) == 0)) {
869                         if (num_devices >= MAX_DEVICES) {
870                                 fprintf(stderr, _("%s: too many devices\n"),
871                                         progname);
872                                 exit(1);
873                         }
874                         devices[num_devices++] =
875                                 interpret_device(string_copy(arg));
876                         continue;
877                 }
878                 if (arg[0] != '-' || opts_for_fsck) {
879                         if (num_args >= MAX_ARGS) {
880                                 fprintf(stderr, _("%s: too many arguments\n"),
881                                         progname);
882                                 exit(1);
883                         }
884                         args[num_args++] = string_copy(arg);
885                         continue;
886                 }
887                 for (j=1; arg[j]; j++) {
888                         if (opts_for_fsck) {
889                                 options[++opt] = arg[j];
890                                 continue;
891                         }
892                         switch (arg[j]) {
893                         case 'A':
894                                 doall++;
895                                 break;
896                         case 'C':
897                                 progress++;
898                                 break;
899                         case 'V':
900                                 verbose++;
901                                 break;
902                         case 'N':
903                                 noexecute++;
904                                 break;
905                         case 'R':
906                                 skip_root++;
907                                 break;
908                         case 'T':
909                                 notitle++;
910                                 break;
911                         case 'M':
912                                 like_mount++;
913                                 break;
914                         case 'P':
915                                 parallel_root++;
916                                 break;
917                         case 's':
918                                 serialize++;
919                                 break;
920                         case 't':
921                                 if (arg[j+1]) {
922                                         fstype = string_copy(arg+j+1);
923                                         goto next_arg;
924                                 }
925                                 if ((i+1) < argc) {
926                                         i++;
927                                         fstype = string_copy(argv[i]);
928                                         goto next_arg;
929                                 }
930                                 usage();
931                                 break;
932                         case '-':
933                                 opts_for_fsck++;
934                                 break;
935                         default:
936                                 options[++opt] = arg[j];
937                                 break;
938                         }
939                 }
940         next_arg:
941                 if (opt) {
942                         options[0] = '-';
943                         options[++opt] = '\0';
944                         if (num_args >= MAX_ARGS) {
945                                 fprintf(stderr,
946                                         _("%s: too many arguments\n"),
947                                         progname);
948                                 exit(1);
949                         }
950                         args[num_args++] = string_copy(options);
951                         opt = 0;
952                 }
953         }
954         if (getenv("FSCK_FORCE_ALL_PARALLEL"))
955                 force_all_parallel++;
956 }
957
958 int main(int argc, char *argv[])
959 {
960         int i;
961         int status = 0;
962         int interactive = 0;
963         char *oldpath = getenv("PATH");
964         const char *fstab;
965
966 #ifdef ENABLE_NLS
967         setlocale(LC_MESSAGES, "");
968         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
969         textdomain(NLS_CAT_NAME);
970 #endif
971         PRS(argc, argv);
972
973         if (!notitle)
974                 printf(_("Parallelizing fsck version %s (%s)\n"),
975                         E2FSPROGS_VERSION, E2FSPROGS_DATE);
976
977         fstab = getenv("FSTAB_FILE");
978         if (!fstab)
979                 fstab = _PATH_MNTTAB;
980         load_fs_info(fstab);
981
982         /* Update our search path to include uncommon directories. */
983         if (oldpath) {
984                 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
985                                     strlen (oldpath) + 1);
986                 strcpy (fsck_path, fsck_prefix_path);
987                 strcat (fsck_path, ":");
988                 strcat (fsck_path, oldpath);
989         } else {
990                 fsck_path = string_copy(fsck_prefix_path);
991         }
992         
993         if ((num_devices == 1) || (serialize))
994                 interactive = 1;
995
996         /* If -A was specified ("check all"), do that! */
997         if (doall)
998                 return check_all();
999
1000         for (i = 0 ; i < num_devices; i++) {
1001                 fsck_device(devices[i], interactive);
1002                 if (serialize) {
1003                         struct fsck_instance *inst;
1004
1005                         inst = wait_one();
1006                         if (inst) {
1007                                 status |= inst->exit_status;
1008                                 free_instance(inst);
1009                         }
1010                 }
1011         }
1012         status |= wait_all();
1013         free(fsck_path);
1014         return status;
1015 }