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