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