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