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