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