1 /*
2  * fsck --- 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  * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com>
23  *
24  * This file may be redistributed under the terms of the GNU Public
25  * License.
26  */
27 
28 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
29 
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33 #include <sys/file.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <time.h>
40 #include <stdlib.h>
41 #include <paths.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <signal.h>
45 #include <dirent.h>
46 #include <sys/resource.h>
47 #include <sys/time.h>
48 #include <blkid.h>
49 #include <libmount.h>
50 
51 #include "nls.h"
52 #include "pathnames.h"
53 #include "exitcodes.h"
54 #include "c.h"
55 #include "closestream.h"
56 #include "fileutils.h"
57 
58 #define XALLOC_EXIT_CODE	FSCK_EX_ERROR
59 #include "xalloc.h"
60 
61 #ifndef DEFAULT_FSTYPE
62 # define DEFAULT_FSTYPE	"ext2"
63 #endif
64 
65 #define MAX_DEVICES 32
66 #define MAX_ARGS 32
67 
68 #define FSCK_RUNTIME_DIRNAME	"/run/fsck"
69 
70 static const char *ignored_types[] = {
71 	"ignore",
72 	"iso9660",
73 	"sw",
74 	NULL
75 };
76 
77 static const char *really_wanted[] = {
78 	"minix",
79 	"ext2",
80 	"ext3",
81 	"ext4",
82 	"ext4dev",
83 	"jfs",
84 	"reiserfs"
85 };
86 
87 /*
88  * Internal structure for mount tabel entries.
89  */
90 struct fsck_fs_data
91 {
92 	const char	*device;
93 	dev_t		disk;
94 	unsigned int	stacked:1,
95 			done:1,
96 			eval_device:1;
97 };
98 
99 /*
100  * Structure to allow exit codes to be stored
101  */
102 struct fsck_instance {
103 	int	pid;
104 	int	flags;		/* FLAG_{DONE|PROGRESS} */
105 
106 	int	lock;		/* flock()ed lockpath file descriptor or -1 */
107 	char	*lockpath;	/* /run/fsck/<diskname>.lock or NULL */
108 
109 	int	exit_status;
110 	struct timeval start_time;
111 	struct timeval end_time;
112 	char *	prog;
113 	char *	type;
114 
115 	struct rusage rusage;
116 	struct libmnt_fs *fs;
117 	struct fsck_instance *next;
118 };
119 
120 #define FLAG_DONE 1
121 #define FLAG_PROGRESS 2
122 
123 /*
124  * Global variables for options
125  */
126 static char *devices[MAX_DEVICES];
127 static char *args[MAX_ARGS];
128 static int num_devices, num_args;
129 
130 static int lockdisk;
131 static int verbose;
132 static int doall;
133 static int noexecute;
134 static int serialize;
135 static int skip_root;
136 static int ignore_mounted;
137 static int notitle;
138 static int parallel_root;
139 static int progress;
140 static int progress_fd;
141 static int force_all_parallel;
142 static int report_stats;
143 
144 static int num_running;
145 static int max_running;
146 
147 static volatile int cancel_requested;
148 static int kill_sent;
149 static char *fstype;
150 static struct fsck_instance *instance_list;
151 
152 static const char fsck_prefix_path[] = FS_SEARCH_PATH;
153 static char *fsck_path;
154 
155 /* parsed fstab and mtab */
156 static struct libmnt_table *fstab, *mtab;
157 static struct libmnt_cache *mntcache;
158 
159 static int count_slaves(dev_t disk);
160 
string_to_int(const char * s)161 static int string_to_int(const char *s)
162 {
163 	long l;
164 	char *p;
165 
166 	l = strtol(s, &p, 0);
167 	if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
168 		return -1;
169 	else
170 		return (int) l;
171 }
172 
173 /* Do we really really want to check this fs? */
fs_check_required(const char * type)174 static int fs_check_required(const char *type)
175 {
176 	size_t i;
177 
178 	for(i = 0; i < ARRAY_SIZE(really_wanted); i++) {
179 		if (strcmp(type, really_wanted[i]) == 0)
180 			return 1;
181 	}
182 
183 	return 0;
184 }
185 
is_mounted(struct libmnt_fs * fs)186 static int is_mounted(struct libmnt_fs *fs)
187 {
188 	int rc;
189 	const char *src;
190 
191 	src = mnt_fs_get_source(fs);
192 	if (!src)
193 		return 0;
194 	if (!mntcache)
195 		mntcache = mnt_new_cache();
196 	if (!mtab) {
197 		mtab = mnt_new_table();
198 		if (!mtab)
199 			err(FSCK_EX_ERROR, ("failed to initialize libmount table"));
200 		mnt_table_set_cache(mtab, mntcache);
201 		mnt_table_parse_mtab(mtab, NULL);
202 	}
203 
204 	rc = mnt_table_find_source(mtab, src, MNT_ITER_BACKWARD) ? 1 : 0;
205 	if (verbose) {
206 		if (rc)
207 			printf(_("%s is mounted\n"), src);
208 		else
209 			printf(_("%s is not mounted\n"), src);
210 	}
211 	return rc;
212 }
213 
214 static int ignore(struct libmnt_fs *);
215 
fs_create_data(struct libmnt_fs * fs)216 static struct fsck_fs_data *fs_create_data(struct libmnt_fs *fs)
217 {
218 	struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
219 
220 	if (!data) {
221 		data = xcalloc(1, sizeof(*data));
222 		mnt_fs_set_userdata(fs, data);
223 	}
224 	return data;
225 }
226 
227 /*
228  * fs from fstab might contains real device name as well as symlink,
229  * LABEL or UUID, this function returns canonicalized result.
230  */
fs_get_device(struct libmnt_fs * fs)231 static const char *fs_get_device(struct libmnt_fs *fs)
232 {
233 	struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
234 
235 	if (!data || !data->eval_device) {
236 		const char *spec = mnt_fs_get_source(fs);
237 
238 		if (!data)
239 			data = fs_create_data(fs);
240 
241 		data->eval_device = 1;
242 		data->device = mnt_resolve_spec(spec, mnt_table_get_cache(fstab));
243 		if (!data->device)
244 			data->device = xstrdup(spec);
245 	}
246 
247 	return data->device;
248 }
249 
fs_get_disk(struct libmnt_fs * fs,int check)250 static dev_t fs_get_disk(struct libmnt_fs *fs, int check)
251 {
252 	struct fsck_fs_data *data;
253 	const char *device;
254 	struct stat st;
255 
256 	data = mnt_fs_get_userdata(fs);
257 	if (data && data->disk)
258 		return data->disk;
259 
260 	if (!check)
261 		return 0;
262 
263 	if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs))
264 		return 0;
265 
266 	device = fs_get_device(fs);
267 	if (!device)
268 		return 0;
269 
270 	data = fs_create_data(fs);
271 
272 	if (!stat(device, &st) &&
273 	    !blkid_devno_to_wholedisk(st.st_rdev, NULL, 0, &data->disk)) {
274 
275 		if (data->disk)
276 			data->stacked = count_slaves(data->disk) > 0 ? 1 : 0;
277 		return data->disk;
278 	}
279 	return 0;
280 }
281 
fs_is_stacked(struct libmnt_fs * fs)282 static int fs_is_stacked(struct libmnt_fs *fs)
283 {
284 	struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
285 	return data ? data->stacked : 0;
286 }
287 
fs_is_done(struct libmnt_fs * fs)288 static int fs_is_done(struct libmnt_fs *fs)
289 {
290 	struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
291 	return data ? data->done : 0;
292 }
293 
fs_set_done(struct libmnt_fs * fs)294 static void fs_set_done(struct libmnt_fs *fs)
295 {
296 	struct fsck_fs_data *data = fs_create_data(fs);
297 
298 	if (data)
299 		data->done = 1;
300 }
301 
is_irrotational_disk(dev_t disk)302 static int is_irrotational_disk(dev_t disk)
303 {
304 	char path[PATH_MAX];
305 	FILE *f;
306 	int rc, x;
307 
308 
309 	rc = snprintf(path, sizeof(path),
310 			"/sys/dev/block/%d:%d/queue/rotational",
311 			major(disk), minor(disk));
312 
313 	if (rc < 0 || (unsigned int) (rc + 1) > sizeof(path))
314 		return 0;
315 
316 	f = fopen(path, "r");
317 	if (!f)
318 		return 0;
319 
320 	rc = fscanf(f, "%d", &x);
321 	if (rc != 1) {
322 		if (ferror(f))
323 			warn(_("cannot read %s"), path);
324 		else
325 			warnx(_("parse error: %s"), path);
326 	}
327 	fclose(f);
328 
329 	return rc == 1 ? !x : 0;
330 }
331 
lock_disk(struct fsck_instance * inst)332 static void lock_disk(struct fsck_instance *inst)
333 {
334 	dev_t disk = fs_get_disk(inst->fs, 1);
335 	char *diskpath = NULL, *diskname;
336 
337 	inst->lock = -1;
338 
339 	if (!disk || is_irrotational_disk(disk))
340 		goto done;
341 
342 	diskpath = blkid_devno_to_devname(disk);
343 	if (!diskpath)
344 		goto done;
345 
346 	if (access(FSCK_RUNTIME_DIRNAME, F_OK) != 0) {
347 		int rc = mkdir(FSCK_RUNTIME_DIRNAME,
348 				    S_IWUSR|
349 				    S_IRUSR|S_IRGRP|S_IROTH|
350 				    S_IXUSR|S_IXGRP|S_IXOTH);
351 		if (rc && errno != EEXIST) {
352 			warn(_("cannot create directory %s"),
353 					FSCK_RUNTIME_DIRNAME);
354 			goto done;
355 		}
356 	}
357 
358 	diskname = stripoff_last_component(diskpath);
359 	if (!diskname)
360 		diskname = diskpath;
361 
362 	xasprintf(&inst->lockpath, FSCK_RUNTIME_DIRNAME "/%s.lock", diskname);
363 
364 	if (verbose)
365 		printf(_("Locking disk by %s ... "), inst->lockpath);
366 
367 	inst->lock = open(inst->lockpath, O_RDONLY|O_CREAT|O_CLOEXEC,
368 				    S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
369 	if (inst->lock >= 0) {
370 		int rc = -1;
371 
372 		/* inform users that we're waiting on the lock */
373 		if (verbose &&
374 		    (rc = flock(inst->lock, LOCK_EX | LOCK_NB)) != 0 &&
375 		    errno == EWOULDBLOCK)
376 			printf(_("(waiting) "));
377 
378 		if (rc != 0 && flock(inst->lock, LOCK_EX) != 0) {
379 			close(inst->lock);			/* failed */
380 			inst->lock = -1;
381 		}
382 	}
383 
384 	if (verbose)
385 		/* TRANSLATORS: These are followups to "Locking disk...". */
386 		printf("%s.\n", inst->lock >= 0 ? _("succeeded") : _("failed"));
387 
388 
389 done:
390 	if (inst->lock < 0) {
391 		free(inst->lockpath);
392 		inst->lockpath = NULL;
393 	}
394 	free(diskpath);
395 	return;
396 }
397 
unlock_disk(struct fsck_instance * inst)398 static void unlock_disk(struct fsck_instance *inst)
399 {
400 	if (inst->lock < 0)
401 		return;
402 
403 	if (verbose)
404 		printf(_("Unlocking %s.\n"), inst->lockpath);
405 
406 	close(inst->lock);			/* unlock */
407 	unlink(inst->lockpath);
408 
409 	free(inst->lockpath);
410 
411 	inst->lock = -1;
412 	inst->lockpath = NULL;
413 }
414 
free_instance(struct fsck_instance * i)415 static void free_instance(struct fsck_instance *i)
416 {
417 	if (lockdisk)
418 		unlock_disk(i);
419 	free(i->prog);
420 	free(i->lockpath);
421 	mnt_unref_fs(i->fs);
422 	free(i);
423 	return;
424 }
425 
add_dummy_fs(const char * device)426 static struct libmnt_fs *add_dummy_fs(const char *device)
427 {
428 	struct libmnt_fs *fs = mnt_new_fs();
429 
430 	if (fs && mnt_fs_set_source(fs, device) == 0 &&
431 				  mnt_table_add_fs(fstab, fs) == 0) {
432 		mnt_unref_fs(fs);
433 		return fs;
434 	}
435 
436 	mnt_unref_fs(fs);
437 	err(FSCK_EX_ERROR, _("failed to setup description for %s"), device);
438 }
439 
fs_interpret_type(struct libmnt_fs * fs)440 static void fs_interpret_type(struct libmnt_fs *fs)
441 {
442 	const char *device;
443 	const char *type = mnt_fs_get_fstype(fs);
444 
445 	if (type && strcmp(type, "auto") != 0)
446 		return;
447 
448 	mnt_fs_set_fstype(fs, NULL);
449 
450 	device = fs_get_device(fs);
451 	if (device) {
452 		int ambi = 0;
453 
454 		type = mnt_get_fstype(device, &ambi, mnt_table_get_cache(fstab));
455 		if (!ambi)
456 			mnt_fs_set_fstype(fs, type);
457 	}
458 }
459 
parser_errcb(struct libmnt_table * tb,const char * filename,int line)460 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
461 			const char *filename, int line)
462 {
463 	warnx(_("%s: parse error at line %d -- ignore"), filename, line);
464 	return 0;
465 }
466 
467 /*
468  * Load the filesystem database from /etc/fstab
469  */
load_fs_info(void)470 static void load_fs_info(void)
471 {
472 	const char *path;
473 
474 	fstab = mnt_new_table();
475 	if (!fstab)
476 		err(FSCK_EX_ERROR, ("failed to initialize libmount table"));
477 
478 	mnt_table_set_parser_errcb(fstab, parser_errcb);
479 	mnt_table_set_cache(fstab, mntcache);
480 
481 	errno = 0;
482 
483 	/*
484 	 * Let's follow libmount defauls if $FSTAB_FILE is not specified
485 	 */
486 	path = getenv("FSTAB_FILE");
487 
488 	if (mnt_table_parse_fstab(fstab, path)) {
489 		if (!path)
490 			path = mnt_get_fstab_path();
491 
492 		/* don't print error when there is no fstab at all */
493 		if (access(path, F_OK) == 0) {
494 			if (errno)
495 				warn(_("%s: failed to parse fstab"), path);
496 			else
497 				warnx(_("%s: failed to parse fstab"), path);
498 		}
499 	}
500 }
501 
502 /*
503  * Lookup filesys in /etc/fstab and return the corresponding entry.
504  * The @path has to be real path (no TAG) by mnt_resolve_spec().
505  */
lookup(char * path)506 static struct libmnt_fs *lookup(char *path)
507 {
508 	struct libmnt_fs *fs;
509 
510 	if (!path)
511 		return NULL;
512 
513 	fs = mnt_table_find_srcpath(fstab, path, MNT_ITER_FORWARD);
514 	if (!fs) {
515 		/*
516 		 * Maybe mountpoint has been specified on fsck command line.
517 		 * Yeah, crazy feature...
518 		 *
519 		 * Note that mnt_table_find_target() may canonicalize paths in
520 		 * the fstab to support symlinks. This is really unwanted,
521 		 * because readlink() on mountpoints may trigger automounts.
522 		 *
523 		 * So, disable the cache and compare the paths as strings
524 		 * without care about symlinks...
525 		 */
526 		mnt_table_set_cache(fstab, NULL);
527 		fs = mnt_table_find_target(fstab, path, MNT_ITER_FORWARD);
528 		mnt_table_set_cache(fstab, mntcache);
529 	}
530 	return fs;
531 }
532 
533 /* Find fsck program for a given fs type. */
find_fsck(const char * type)534 static char *find_fsck(const char *type)
535 {
536 	char *s;
537 	const char *tpl;
538 	static char prog[256];
539 	char *p = xstrdup(fsck_path);
540 	struct stat st;
541 
542 	/* Are we looking for a program or just a type? */
543 	tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
544 
545 	for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
546 		sprintf(prog, tpl, s, type);
547 		if (stat(prog, &st) == 0)
548 			break;
549 	}
550 	free(p);
551 
552 	return(s ? prog : NULL);
553 }
554 
progress_active(void)555 static int progress_active(void)
556 {
557 	struct fsck_instance *inst;
558 
559 	for (inst = instance_list; inst; inst = inst->next) {
560 		if (inst->flags & FLAG_DONE)
561 			continue;
562 		if (inst->flags & FLAG_PROGRESS)
563 			return 1;
564 	}
565 	return 0;
566 }
567 
568 /*
569  * Process run statistics for finished fsck instances.
570  *
571  * If report_stats is 0, do nothing, otherwise print a selection of
572  * interesting rusage statistics as well as elapsed wallclock time.
573  */
print_stats(struct fsck_instance * inst)574 static void print_stats(struct fsck_instance *inst)
575 {
576 	double time_diff;
577 
578 	if (!inst || !report_stats || noexecute)
579 		return;
580 
581 	time_diff = (inst->end_time.tv_sec  - inst->start_time.tv_sec)
582 		  + (inst->end_time.tv_usec - inst->start_time.tv_usec) / 1E6;
583 
584 	fprintf(stdout, "%s: status %d, rss %ld, "
585 			"real %f, user %d.%06d, sys %d.%06d\n",
586 		fs_get_device(inst->fs),
587 		inst->exit_status,
588 		inst->rusage.ru_maxrss,
589 		time_diff,
590 		(int)inst->rusage.ru_utime.tv_sec,
591 		(int)inst->rusage.ru_utime.tv_usec,
592 		(int)inst->rusage.ru_stime.tv_sec,
593 		(int)inst->rusage.ru_stime.tv_usec);
594 }
595 
596 /*
597  * Execute a particular fsck program, and link it into the list of
598  * child processes we are waiting for.
599  */
execute(const char * progname,const char * progpath,const char * type,struct libmnt_fs * fs,int interactive)600 static int execute(const char *progname, const char *progpath,
601 		   const char *type, struct libmnt_fs *fs, int interactive)
602 {
603 	char *argv[80];
604 	int  argc, i;
605 	struct fsck_instance *inst, *p;
606 	pid_t	pid;
607 
608 	inst = xcalloc(1, sizeof(*inst));
609 
610 	argv[0] = xstrdup(progname);
611 	argc = 1;
612 
613 	for (i=0; i <num_args; i++)
614 		argv[argc++] = xstrdup(args[i]);
615 
616 	if (progress) {
617 		if ((strcmp(type, "ext2") == 0) ||
618 		    (strcmp(type, "ext3") == 0) ||
619 		    (strcmp(type, "ext4") == 0) ||
620 		    (strcmp(type, "ext4dev") == 0)) {
621 			char tmp[80];
622 
623 			tmp[0] = 0;
624 			if (!progress_active()) {
625 				snprintf(tmp, 80, "-C%d", progress_fd);
626 				inst->flags |= FLAG_PROGRESS;
627 			} else if (progress_fd)
628 				snprintf(tmp, 80, "-C%d", progress_fd * -1);
629 			if (tmp[0])
630 				argv[argc++] = xstrdup(tmp);
631 		}
632 	}
633 
634 	argv[argc++] = xstrdup(fs_get_device(fs));
635 	argv[argc] = 0;
636 
637 	if (verbose || noexecute) {
638 		const char *tgt = mnt_fs_get_target(fs);
639 
640 		if (!tgt)
641 			tgt = fs_get_device(fs);
642 		printf("[%s (%d) -- %s] ", progpath, num_running, tgt);
643 		for (i=0; i < argc; i++)
644 			printf("%s ", argv[i]);
645 		printf("\n");
646 	}
647 
648 	mnt_ref_fs(fs);
649 	inst->fs = fs;
650 	inst->lock = -1;
651 
652 	if (lockdisk)
653 		lock_disk(inst);
654 
655 	/* Fork and execute the correct program. */
656 	if (noexecute)
657 		pid = -1;
658 	else if ((pid = fork()) < 0) {
659 		warn(_("fork failed"));
660 		free_instance(inst);
661 		return errno;
662 	} else if (pid == 0) {
663 		if (!interactive)
664 			close(0);
665 		execv(progpath, argv);
666 		err(FSCK_EX_ERROR, _("%s: execute failed"), progpath);
667 	}
668 
669 	for (i=0; i < argc; i++)
670 		free(argv[i]);
671 
672 	inst->pid = pid;
673 	inst->prog = xstrdup(progname);
674 	inst->type = xstrdup(type);
675 	gettimeofday(&inst->start_time, NULL);
676 	inst->next = NULL;
677 
678 	/*
679 	 * Find the end of the list, so we add the instance on at the end.
680 	 */
681 	for (p = instance_list; p && p->next; p = p->next);
682 
683 	if (p)
684 		p->next = inst;
685 	else
686 		instance_list = inst;
687 
688 	return 0;
689 }
690 
691 /*
692  * Send a signal to all outstanding fsck child processes
693  */
kill_all(int signum)694 static int kill_all(int signum)
695 {
696 	struct fsck_instance *inst;
697 	int	n = 0;
698 
699 	for (inst = instance_list; inst; inst = inst->next) {
700 		if (inst->flags & FLAG_DONE)
701 			continue;
702 		kill(inst->pid, signum);
703 		n++;
704 	}
705 	return n;
706 }
707 
708 /*
709  * Wait for one child process to exit; when it does, unlink it from
710  * the list of executing child processes, and return it.
711  */
wait_one(int flags)712 static struct fsck_instance *wait_one(int flags)
713 {
714 	int	status = 0;
715 	int	sig;
716 	struct fsck_instance *inst, *inst2, *prev;
717 	pid_t	pid;
718 	struct rusage rusage;
719 
720 	if (!instance_list)
721 		return NULL;
722 
723 	if (noexecute) {
724 		inst = instance_list;
725 		prev = 0;
726 #ifdef RANDOM_DEBUG
727 		while (inst->next && (random() & 1)) {
728 			prev = inst;
729 			inst = inst->next;
730 		}
731 #endif
732 		inst->exit_status = 0;
733 		goto ret_inst;
734 	}
735 
736 	/*
737 	 * gcc -Wall fails saving throw against stupidity
738 	 * (inst and prev are thought to be uninitialized variables)
739 	 */
740 	inst = prev = NULL;
741 
742 	do {
743 		pid = wait4(-1, &status, flags, &rusage);
744 		if (cancel_requested && !kill_sent) {
745 			kill_all(SIGTERM);
746 			kill_sent++;
747 		}
748 		if ((pid == 0) && (flags & WNOHANG))
749 			return NULL;
750 		if (pid < 0) {
751 			if ((errno == EINTR) || (errno == EAGAIN))
752 				continue;
753 			if (errno == ECHILD) {
754 				warnx(_("wait: no more child process?!?"));
755 				return NULL;
756 			}
757 			warn(_("waitpid failed"));
758 			continue;
759 		}
760 		for (prev = 0, inst = instance_list;
761 		     inst;
762 		     prev = inst, inst = inst->next) {
763 			if (inst->pid == pid)
764 				break;
765 		}
766 	} while (!inst);
767 
768 	if (WIFEXITED(status))
769 		status = WEXITSTATUS(status);
770 	else if (WIFSIGNALED(status)) {
771 		sig = WTERMSIG(status);
772 		if (sig == SIGINT) {
773 			status = FSCK_EX_UNCORRECTED;
774 		} else {
775 			warnx(_("Warning... %s for device %s exited "
776 			       "with signal %d."),
777 			       inst->prog, fs_get_device(inst->fs), sig);
778 			status = FSCK_EX_ERROR;
779 		}
780 	} else {
781 		warnx(_("%s %s: status is %x, should never happen."),
782 		       inst->prog, fs_get_device(inst->fs), status);
783 		status = FSCK_EX_ERROR;
784 	}
785 
786 	inst->exit_status = status;
787 	inst->flags |= FLAG_DONE;
788 	gettimeofday(&inst->end_time, NULL);
789 	memcpy(&inst->rusage, &rusage, sizeof(struct rusage));
790 
791 	if (progress && (inst->flags & FLAG_PROGRESS) &&
792 	    !progress_active()) {
793 		for (inst2 = instance_list; inst2; inst2 = inst2->next) {
794 			if (inst2->flags & FLAG_DONE)
795 				continue;
796 			if (strcmp(inst2->type, "ext2") &&
797 			    strcmp(inst2->type, "ext3") &&
798 			    strcmp(inst2->type, "ext4") &&
799 			    strcmp(inst2->type, "ext4dev"))
800 				continue;
801 			/*
802 			 * If we've just started the fsck, wait a tiny
803 			 * bit before sending the kill, to give it
804 			 * time to set up the signal handler
805 			 */
806 			if (inst2->start_time.tv_sec < time(0) + 2) {
807 				if (fork() == 0) {
808 					sleep(1);
809 					kill(inst2->pid, SIGUSR1);
810 					exit(FSCK_EX_OK);
811 				}
812 			} else
813 				kill(inst2->pid, SIGUSR1);
814 			inst2->flags |= FLAG_PROGRESS;
815 			break;
816 		}
817 	}
818 ret_inst:
819 	if (prev)
820 		prev->next = inst->next;
821 	else
822 		instance_list = inst->next;
823 
824 	print_stats(inst);
825 
826 	if (verbose > 1)
827 		printf(_("Finished with %s (exit status %d)\n"),
828 		       fs_get_device(inst->fs), inst->exit_status);
829 	num_running--;
830 	return inst;
831 }
832 
833 #define FLAG_WAIT_ALL		0
834 #define FLAG_WAIT_ATLEAST_ONE	1
835 /*
836  * Wait until all executing child processes have exited; return the
837  * logical OR of all of their exit code values.
838  */
wait_many(int flags)839 static int wait_many(int flags)
840 {
841 	struct fsck_instance *inst;
842 	int	global_status = 0;
843 	int	wait_flags = 0;
844 
845 	while ((inst = wait_one(wait_flags))) {
846 		global_status |= inst->exit_status;
847 		free_instance(inst);
848 #ifdef RANDOM_DEBUG
849 		if (noexecute && (flags & WNOHANG) && !(random() % 3))
850 			break;
851 #endif
852 		if (flags & FLAG_WAIT_ATLEAST_ONE)
853 			wait_flags = WNOHANG;
854 	}
855 	return global_status;
856 }
857 
858 /*
859  * Run the fsck program on a particular device
860  *
861  * If the type is specified using -t, and it isn't prefixed with "no"
862  * (as in "noext2") and only one filesystem type is specified, then
863  * use that type regardless of what is specified in /etc/fstab.
864  *
865  * If the type isn't specified by the user, then use either the type
866  * specified in /etc/fstab, or DEFAULT_FSTYPE.
867  */
fsck_device(struct libmnt_fs * fs,int interactive)868 static int fsck_device(struct libmnt_fs *fs, int interactive)
869 {
870 	char progname[80], *progpath;
871 	const char *type;
872 	int retval;
873 
874 	fs_interpret_type(fs);
875 
876 	type = mnt_fs_get_fstype(fs);
877 
878 	if (type && strcmp(type, "auto") != 0)
879 		;
880 	else if (fstype && strncmp(fstype, "no", 2) &&
881 	    strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
882 	    !strchr(fstype, ','))
883 		type = fstype;
884 	else
885 		type = DEFAULT_FSTYPE;
886 
887 	sprintf(progname, "fsck.%s", type);
888 	progpath = find_fsck(progname);
889 	if (progpath == NULL) {
890 		if (fs_check_required(type)) {
891 			retval = ENOENT;
892 			goto err;
893 		}
894 		return 0;
895 	}
896 
897 	num_running++;
898 	retval = execute(progname, progpath, type, fs, interactive);
899 	if (retval) {
900 		num_running--;
901 		goto err;
902 	}
903 	return 0;
904 err:
905 	warnx(_("error %d (%m) while executing fsck.%s for %s"),
906 			retval, type, fs_get_device(fs));
907 	return FSCK_EX_ERROR;
908 }
909 
910 
911 /*
912  * Deal with the fsck -t argument.
913  */
914 struct fs_type_compile {
915 	char **list;
916 	int *type;
917 	int  negate;
918 } fs_type_compiled;
919 
920 #define FS_TYPE_NORMAL	0
921 #define FS_TYPE_OPT	1
922 #define FS_TYPE_NEGOPT	2
923 
compile_fs_type(char * fs_type,struct fs_type_compile * cmp)924 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
925 {
926 	char	*cp, *list, *s;
927 	int	num = 2;
928 	int	negate, first_negate = 1;
929 
930 	if (fs_type) {
931 		for (cp=fs_type; *cp; cp++) {
932 			if (*cp == ',')
933 				num++;
934 		}
935 	}
936 
937 	cmp->list = xcalloc(num, sizeof(char *));
938 	cmp->type = xcalloc(num, sizeof(int));
939 	cmp->negate = 0;
940 
941 	if (!fs_type)
942 		return;
943 
944 	list = xstrdup(fs_type);
945 	num = 0;
946 	s = strtok(list, ",");
947 	while(s) {
948 		negate = 0;
949 		if (strncmp(s, "no", 2) == 0) {
950 			s += 2;
951 			negate = 1;
952 		} else if (*s == '!') {
953 			s++;
954 			negate = 1;
955 		}
956 		if (strcmp(s, "loop") == 0)
957 			/* loop is really short-hand for opts=loop */
958 			goto loop_special_case;
959 		else if (strncmp(s, "opts=", 5) == 0) {
960 			s += 5;
961 		loop_special_case:
962 			cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
963 		} else {
964 			if (first_negate) {
965 				cmp->negate = negate;
966 				first_negate = 0;
967 			}
968 			if ((negate && !cmp->negate) ||
969 			    (!negate && cmp->negate)) {
970 				errx(FSCK_EX_USAGE,
971 					_("Either all or none of the filesystem types passed to -t must be prefixed\n"
972 					  "with 'no' or '!'."));
973 			}
974 		}
975 
976 		cmp->list[num++] = xstrdup(s);
977 		s = strtok(NULL, ",");
978 	}
979 	free(list);
980 }
981 
982 /*
983  * This function returns true if a particular option appears in a
984  * comma-delimited options list
985  */
opt_in_list(const char * opt,const char * optlist)986 static int opt_in_list(const char *opt, const char *optlist)
987 {
988 	char	*list, *s;
989 
990 	if (!optlist)
991 		return 0;
992 	list = xstrdup(optlist);
993 
994 	s = strtok(list, ",");
995 	while(s) {
996 		if (strcmp(s, opt) == 0) {
997 			free(list);
998 			return 1;
999 		}
1000 		s = strtok(NULL, ",");
1001 	}
1002 	free(list);
1003 	return 0;
1004 }
1005 
1006 /* See if the filesystem matches the criteria given by the -t option */
fs_match(struct libmnt_fs * fs,struct fs_type_compile * cmp)1007 static int fs_match(struct libmnt_fs *fs, struct fs_type_compile *cmp)
1008 {
1009 	int n, ret = 0, checked_type = 0;
1010 	char *cp;
1011 
1012 	if (cmp->list == 0 || cmp->list[0] == 0)
1013 		return 1;
1014 
1015 	for (n=0; (cp = cmp->list[n]); n++) {
1016 		switch (cmp->type[n]) {
1017 		case FS_TYPE_NORMAL:
1018 		{
1019 			const char *type = mnt_fs_get_fstype(fs);
1020 
1021 			checked_type++;
1022 			if (type && strcmp(cp, type) == 0)
1023 				ret = 1;
1024 			break;
1025 		}
1026 		case FS_TYPE_NEGOPT:
1027 			if (opt_in_list(cp, mnt_fs_get_options(fs)))
1028 				return 0;
1029 			break;
1030 		case FS_TYPE_OPT:
1031 			if (!opt_in_list(cp, mnt_fs_get_options(fs)))
1032 				return 0;
1033 			break;
1034 		}
1035 	}
1036 	if (checked_type == 0)
1037 		return 1;
1038 	return (cmp->negate ? !ret : ret);
1039 }
1040 
1041 /*
1042  * Check if a device exists
1043  */
device_exists(const char * device)1044 static int device_exists(const char *device)
1045 {
1046 	struct stat st;
1047 
1048 	if (stat(device, &st) == -1)
1049 		return 0;
1050 	if (!S_ISBLK(st.st_mode))
1051 		return 0;
1052 	return 1;
1053 }
1054 
fs_ignored_type(struct libmnt_fs * fs)1055 static int fs_ignored_type(struct libmnt_fs *fs)
1056 {
1057 	const char **ip, *type;
1058 
1059 	if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs) || mnt_fs_is_swaparea(fs))
1060 		return 1;
1061 
1062 	type = mnt_fs_get_fstype(fs);
1063 
1064 	for(ip = ignored_types; type && *ip; ip++) {
1065 		if (strcmp(type, *ip) == 0)
1066 			return 1;
1067 	}
1068 
1069 	return 0;
1070 }
1071 
1072 /* Check if we should ignore this filesystem. */
ignore(struct libmnt_fs * fs)1073 static int ignore(struct libmnt_fs *fs)
1074 {
1075 	const char *type;
1076 
1077 	/*
1078 	 * If the pass number is 0, ignore it.
1079 	 */
1080 	if (mnt_fs_get_passno(fs) == 0)
1081 		return 1;
1082 
1083 	/*
1084 	 * If this is a bind mount, ignore it.
1085 	 */
1086 	if (opt_in_list("bind", mnt_fs_get_options(fs))) {
1087 		warnx(_("%s: skipping bad line in /etc/fstab: "
1088 			"bind mount with nonzero fsck pass number"),
1089 			mnt_fs_get_target(fs));
1090 		return 1;
1091 	}
1092 
1093 	/*
1094 	 * ignore devices that don't exist and have the "nofail" mount option
1095 	 */
1096 	if (!device_exists(fs_get_device(fs))) {
1097 		if (opt_in_list("nofail", mnt_fs_get_options(fs))) {
1098 			if (verbose)
1099 				printf(_("%s: skipping nonexistent device\n"),
1100 							fs_get_device(fs));
1101 			return 1;
1102 		}
1103 		if (verbose)
1104 			printf(_("%s: nonexistent device (\"nofail\" fstab "
1105 				 "option may be used to skip this device)\n"),
1106 				fs_get_device(fs));
1107 	}
1108 
1109 	fs_interpret_type(fs);
1110 
1111 	/*
1112 	 * If a specific fstype is specified, and it doesn't match,
1113 	 * ignore it.
1114 	 */
1115 	if (!fs_match(fs, &fs_type_compiled))
1116 		return 1;
1117 
1118 	type = mnt_fs_get_fstype(fs);
1119 	if (!type) {
1120 		if (verbose)
1121 			printf(_("%s: skipping unknown filesystem type\n"),
1122 				fs_get_device(fs));
1123 		return 1;
1124 	}
1125 
1126 	/* Are we ignoring this type? */
1127 	if (fs_ignored_type(fs))
1128 		return 1;
1129 
1130 
1131 
1132 	/* See if the <fsck.fs> program is available. */
1133 	if (find_fsck(type) == NULL) {
1134 		if (fs_check_required(type))
1135 			warnx(_("cannot check %s: fsck.%s not found"),
1136 				fs_get_device(fs), type);
1137 		return 1;
1138 	}
1139 
1140 	/* We can and want to check this file system type. */
1141 	return 0;
1142 }
1143 
count_slaves(dev_t disk)1144 static int count_slaves(dev_t disk)
1145 {
1146 	DIR *dir;
1147 	struct dirent *dp;
1148 	char dirname[PATH_MAX];
1149 	int count = 0;
1150 
1151 	snprintf(dirname, sizeof(dirname),
1152 			"/sys/dev/block/%u:%u/slaves/",
1153 			major(disk), minor(disk));
1154 
1155 	if (!(dir = opendir(dirname)))
1156 		return -1;
1157 
1158 	while ((dp = readdir(dir)) != 0) {
1159 #ifdef _DIRENT_HAVE_D_TYPE
1160 		if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
1161 			continue;
1162 #endif
1163 		if (dp->d_name[0] == '.' &&
1164 		    ((dp->d_name[1] == 0) ||
1165 		     ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
1166 			continue;
1167 
1168 		count++;
1169 	}
1170 
1171 	closedir(dir);
1172 	return count;
1173 }
1174 
1175 /*
1176  * Returns TRUE if a partition on the same disk is already being
1177  * checked.
1178  */
disk_already_active(struct libmnt_fs * fs)1179 static int disk_already_active(struct libmnt_fs *fs)
1180 {
1181 	struct fsck_instance *inst;
1182 	dev_t disk;
1183 
1184 	if (force_all_parallel)
1185 		return 0;
1186 
1187 	if (instance_list && fs_is_stacked(instance_list->fs))
1188 		/* any instance for a stacked device is already running */
1189 		return 1;
1190 
1191 	disk = fs_get_disk(fs, 1);
1192 
1193 	/*
1194 	 * If we don't know the base device, assume that the device is
1195 	 * already active if there are any fsck instances running.
1196 	 *
1197 	 * Don't check a stacked device with any other disk too.
1198 	 */
1199 	if (!disk || fs_is_stacked(fs))
1200 		return (instance_list != 0);
1201 
1202 	for (inst = instance_list; inst; inst = inst->next) {
1203 		dev_t idisk = fs_get_disk(inst->fs, 0);
1204 
1205 		if (!idisk || disk == idisk)
1206 			return 1;
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 /* Check all file systems, using the /etc/fstab table. */
check_all(void)1213 static int check_all(void)
1214 {
1215 	int not_done_yet = 1;
1216 	int passno = 1;
1217 	int pass_done;
1218 	int status = FSCK_EX_OK;
1219 
1220 	struct libmnt_fs *fs;
1221 	struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
1222 
1223 	if (!itr)
1224 		err(FSCK_EX_ERROR, _("failed to allocate iterator"));
1225 
1226 	/*
1227 	 * Do an initial scan over the filesystem; mark filesystems
1228 	 * which should be ignored as done, and resolve any "auto"
1229 	 * filesystem types (done as a side-effect of calling ignore()).
1230 	 */
1231 	while (mnt_table_next_fs(fstab, itr, &fs) == 0) {
1232 		if (ignore(fs)) {
1233 			fs_set_done(fs);
1234 			continue;
1235 		}
1236 	}
1237 
1238 	if (verbose)
1239 		fputs(_("Checking all file systems.\n"), stdout);
1240 
1241 	/*
1242 	 * Find and check the root filesystem.
1243 	 */
1244 	if (!parallel_root) {
1245 		fs = mnt_table_find_target(fstab, "/", MNT_ITER_FORWARD);
1246 		if (fs) {
1247 			if (!skip_root &&
1248 			    !fs_is_done(fs) &&
1249 			    !(ignore_mounted && is_mounted(fs))) {
1250 				status |= fsck_device(fs, 1);
1251 				status |= wait_many(FLAG_WAIT_ALL);
1252 				if (status > FSCK_EX_NONDESTRUCT) {
1253 					mnt_free_iter(itr);
1254 					return status;
1255 				}
1256 			}
1257 			fs_set_done(fs);
1258 		}
1259 	}
1260 
1261 	/*
1262 	 * This is for the bone-headed user who enters the root
1263 	 * filesystem twice.  Skip root will skep all root entries.
1264 	 */
1265 	if (skip_root) {
1266 		mnt_reset_iter(itr, MNT_ITER_FORWARD);
1267 
1268 		while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1269 			const char *tgt = mnt_fs_get_target(fs);
1270 
1271 			if (tgt && strcmp(tgt, "/") == 0)
1272 				fs_set_done(fs);
1273 		}
1274 	}
1275 
1276 	while (not_done_yet) {
1277 		not_done_yet = 0;
1278 		pass_done = 1;
1279 
1280 		mnt_reset_iter(itr, MNT_ITER_FORWARD);
1281 
1282 		while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1283 
1284 			if (cancel_requested)
1285 				break;
1286 			if (fs_is_done(fs))
1287 				continue;
1288 			/*
1289 			 * If the filesystem's pass number is higher
1290 			 * than the current pass number, then we don't
1291 			 * do it yet.
1292 			 */
1293 			if (mnt_fs_get_passno(fs) > passno) {
1294 				not_done_yet++;
1295 				continue;
1296 			}
1297 			if (ignore_mounted && is_mounted(fs)) {
1298 				fs_set_done(fs);
1299 				continue;
1300 			}
1301 			/*
1302 			 * If a filesystem on a particular device has
1303 			 * already been spawned, then we need to defer
1304 			 * this to another pass.
1305 			 */
1306 			if (disk_already_active(fs)) {
1307 				pass_done = 0;
1308 				continue;
1309 			}
1310 			/*
1311 			 * Spawn off the fsck process
1312 			 */
1313 			status |= fsck_device(fs, serialize);
1314 			fs_set_done(fs);
1315 
1316 			/*
1317 			 * Only do one filesystem at a time, or if we
1318 			 * have a limit on the number of fsck's extant
1319 			 * at one time, apply that limit.
1320 			 */
1321 			if (serialize ||
1322 			    (max_running && (num_running >= max_running))) {
1323 				pass_done = 0;
1324 				break;
1325 			}
1326 		}
1327 		if (cancel_requested)
1328 			break;
1329 		if (verbose > 1)
1330 			printf(_("--waiting-- (pass %d)\n"), passno);
1331 
1332 		status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1333 				    FLAG_WAIT_ATLEAST_ONE);
1334 		if (pass_done) {
1335 			if (verbose > 1)
1336 				printf("----------------------------------\n");
1337 			passno++;
1338 		} else
1339 			not_done_yet++;
1340 	}
1341 
1342 	if (cancel_requested && !kill_sent) {
1343 		kill_all(SIGTERM);
1344 		kill_sent++;
1345 	}
1346 
1347 	status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1348 	mnt_free_iter(itr);
1349 	return status;
1350 }
1351 
usage(FILE * out)1352 static void __attribute__((__noreturn__)) usage(FILE *out)
1353 {
1354 	fputs(USAGE_HEADER, out);
1355 	fprintf(out, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1356 			 program_invocation_short_name);
1357 
1358 	fputs(USAGE_OPTIONS, out);
1359 	fputs(_(" -A         check all filesystems\n"), out);
1360 	fputs(_(" -C [<fd>]  display progress bar; file descriptor is for GUIs\n"), out);
1361 	fputs(_(" -l         lock the device to guarantee exclusive access\n"), out);
1362 	fputs(_(" -M         do not check mounted filesystems\n"), out);
1363 	fputs(_(" -N         do not execute, just show what would be done\n"), out);
1364 	fputs(_(" -P         check filesystems in parallel, including root\n"), out);
1365 	fputs(_(" -R         skip root filesystem; useful only with '-A'\n"), out);
1366 	fputs(_(" -r         report statistics for each device checked\n"), out);
1367 	fputs(_(" -s         serialize the checking operations\n"), out);
1368 	fputs(_(" -T         do not show the title on startup\n"), out);
1369 	fputs(_(" -t <type>  specify filesystem types to be checked;\n"
1370 		"             <type> is allowed to be a comma-separated list\n"), out);
1371 	fputs(_(" -V         explain what is being done\n"), out);
1372 	fputs(_(" -?         display this help and exit\n"), out);
1373 
1374 	fputs(USAGE_SEPARATOR, out);
1375 	fputs(_("See the specific fsck.* commands for available fs-options."), out);
1376 	fprintf(out, USAGE_MAN_TAIL("fsck(8)"));
1377 
1378 	exit(out == stderr ? FSCK_EX_USAGE : FSCK_EX_OK);
1379 }
1380 
signal_cancel(int sig)1381 static void signal_cancel(int sig __attribute__((__unused__)))
1382 {
1383 	cancel_requested++;
1384 }
1385 
parse_argv(int argc,char * argv[])1386 static void parse_argv(int argc, char *argv[])
1387 {
1388 	int	i, j;
1389 	char	*arg, *dev, *tmp = 0;
1390 	char	options[128];
1391 	int	opt = 0;
1392 	int     opts_for_fsck = 0;
1393 	struct sigaction	sa;
1394 
1395 	/*
1396 	 * Set up signal action
1397 	 */
1398 	memset(&sa, 0, sizeof(struct sigaction));
1399 	sa.sa_handler = signal_cancel;
1400 	sigaction(SIGINT, &sa, 0);
1401 	sigaction(SIGTERM, &sa, 0);
1402 
1403 	num_devices = 0;
1404 	num_args = 0;
1405 	instance_list = 0;
1406 
1407 	for (i=1; i < argc; i++) {
1408 		arg = argv[i];
1409 		if (!arg)
1410 			continue;
1411 		if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1412 			if (num_devices >= MAX_DEVICES)
1413 				errx(FSCK_EX_ERROR, _("too many devices"));
1414 
1415 			dev = mnt_resolve_spec(arg, mntcache);
1416 
1417 			if (!dev && strchr(arg, '=')) {
1418 				/*
1419 				 * Check to see if we failed because
1420 				 * /proc/partitions isn't found.
1421 				 */
1422 				if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) {
1423 					warn(_("cannot open %s"),
1424 						_PATH_PROC_PARTITIONS);
1425 					errx(FSCK_EX_ERROR, _("Is /proc mounted?"));
1426 				}
1427 				/*
1428 				 * Check to see if this is because
1429 				 * we're not running as root
1430 				 */
1431 				if (geteuid())
1432 					errx(FSCK_EX_ERROR,
1433 						_("must be root to scan for matching filesystems: %s"),
1434 						arg);
1435 				else
1436 					errx(FSCK_EX_ERROR,
1437 						_("couldn't find matching filesystem: %s"),
1438 						arg);
1439 			}
1440 			devices[num_devices++] = dev ? dev : xstrdup(arg);
1441 			continue;
1442 		}
1443 		if (arg[0] != '-' || opts_for_fsck) {
1444 			if (num_args >= MAX_ARGS)
1445 				errx(FSCK_EX_ERROR, _("too many arguments"));
1446 			args[num_args++] = xstrdup(arg);
1447 			continue;
1448 		}
1449 		for (j=1; arg[j]; j++) {
1450 			if (opts_for_fsck) {
1451 				options[++opt] = arg[j];
1452 				continue;
1453 			}
1454 			switch (arg[j]) {
1455 			case 'A':
1456 				doall = 1;
1457 				break;
1458 			case 'C':
1459 				progress = 1;
1460 				if (arg[j+1]) {
1461 					progress_fd = string_to_int(arg+j+1);
1462 					if (progress_fd < 0)
1463 						progress_fd = 0;
1464 					else
1465 						goto next_arg;
1466 				} else if ((i+1) < argc &&
1467 					   !strncmp(argv[i+1], "-", 1) == 0) {
1468 					progress_fd = string_to_int(argv[i]);
1469 					if (progress_fd < 0)
1470 						progress_fd = 0;
1471 					else {
1472 						++i;
1473 						goto next_arg;
1474 					}
1475 				}
1476 				break;
1477 			case 'l':
1478 				lockdisk = 1;
1479 				break;
1480 			case 'V':
1481 				verbose++;
1482 				break;
1483 			case 'N':
1484 				noexecute = 1;
1485 				break;
1486 			case 'R':
1487 				skip_root = 1;
1488 				break;
1489 			case 'T':
1490 				notitle = 1;
1491 				break;
1492 			case 'M':
1493 				ignore_mounted = 1;
1494 				break;
1495 			case 'P':
1496 				parallel_root = 1;
1497 				break;
1498 			case 'r':
1499 				report_stats = 1;
1500 				break;
1501 			case 's':
1502 				serialize = 1;
1503 				break;
1504 			case 't':
1505 				tmp = 0;
1506 				if (fstype)
1507 					usage(stderr);
1508 				if (arg[j+1])
1509 					tmp = arg+j+1;
1510 				else if ((i+1) < argc)
1511 					tmp = argv[++i];
1512 				else
1513 					usage(stderr);
1514 				fstype = xstrdup(tmp);
1515 				compile_fs_type(fstype, &fs_type_compiled);
1516 				goto next_arg;
1517 			case '-':
1518 				opts_for_fsck++;
1519 				break;
1520 			case '?':
1521 				usage(stdout);
1522 				break;
1523 			default:
1524 				options[++opt] = arg[j];
1525 				break;
1526 			}
1527 		}
1528 	next_arg:
1529 		if (opt) {
1530 			options[0] = '-';
1531 			options[++opt] = '\0';
1532 			if (num_args >= MAX_ARGS)
1533 				errx(FSCK_EX_ERROR, _("too many arguments"));
1534 			args[num_args++] = xstrdup(options);
1535 			opt = 0;
1536 		}
1537 	}
1538 	if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1539 		force_all_parallel++;
1540 	if ((tmp = getenv("FSCK_MAX_INST")))
1541 	    max_running = atoi(tmp);
1542 }
1543 
main(int argc,char * argv[])1544 int main(int argc, char *argv[])
1545 {
1546 	int i, status = 0;
1547 	int interactive = 0;
1548 	char *oldpath = getenv("PATH");
1549 	struct libmnt_fs *fs;
1550 
1551 	setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1552 	setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1553 
1554 	setlocale(LC_MESSAGES, "");
1555 	setlocale(LC_CTYPE, "");
1556 	bindtextdomain(PACKAGE, LOCALEDIR);
1557 	textdomain(PACKAGE);
1558 	atexit(close_stdout);
1559 
1560 	mnt_init_debug(0);		/* init libmount debug mask */
1561 	mntcache = mnt_new_cache();	/* no fatal error if failed */
1562 
1563 	parse_argv(argc, argv);
1564 
1565 	if (!notitle)
1566 		printf(UTIL_LINUX_VERSION);
1567 
1568 	load_fs_info();
1569 
1570 	/* Update our search path to include uncommon directories. */
1571 	if (oldpath) {
1572 		fsck_path = xmalloc (strlen (fsck_prefix_path) + 1 +
1573 				    strlen (oldpath) + 1);
1574 		strcpy (fsck_path, fsck_prefix_path);
1575 		strcat (fsck_path, ":");
1576 		strcat (fsck_path, oldpath);
1577 	} else {
1578 		fsck_path = xstrdup(fsck_prefix_path);
1579 	}
1580 
1581 	if ((num_devices == 1) || (serialize))
1582 		interactive = 1;
1583 
1584 	if (lockdisk && (doall || num_devices > 1)) {
1585 		warnx(_("the -l option can be used with one "
1586 				  "device only -- ignore"));
1587 		lockdisk = 0;
1588 	}
1589 
1590 	/* If -A was specified ("check all"), do that! */
1591 	if (doall)
1592 		return check_all();
1593 
1594 	if (num_devices == 0) {
1595 		serialize++;
1596 		interactive++;
1597 		return check_all();
1598 	}
1599 	for (i = 0 ; i < num_devices; i++) {
1600 		if (cancel_requested) {
1601 			if (!kill_sent) {
1602 				kill_all(SIGTERM);
1603 				kill_sent++;
1604 			}
1605 			break;
1606 		}
1607 		fs = lookup(devices[i]);
1608 		if (!fs)
1609 			fs = add_dummy_fs(devices[i]);
1610 		else if (fs_ignored_type(fs))
1611 			continue;
1612 		if (ignore_mounted && is_mounted(fs))
1613 			continue;
1614 		status |= fsck_device(fs, interactive);
1615 		if (serialize ||
1616 		    (max_running && (num_running >= max_running))) {
1617 			struct fsck_instance *inst;
1618 
1619 			inst = wait_one(0);
1620 			if (inst) {
1621 				status |= inst->exit_status;
1622 				free_instance(inst);
1623 			}
1624 			if (verbose > 1)
1625 				printf("----------------------------------\n");
1626 		}
1627 	}
1628 	status |= wait_many(FLAG_WAIT_ALL);
1629 	free(fsck_path);
1630 	mnt_unref_cache(mntcache);
1631 	mnt_unref_table(fstab);
1632 	mnt_unref_table(mtab);
1633 	return status;
1634 }
1635