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