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