]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/fsck.c
fsck: check errno after strto..()
[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
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;
733 kill(inst->pid, signum);
734 n++;
735 }
736 return n;
737}
738
739/*
740 * Wait for one child process to exit; when it does, unlink it from
741 * the list of executing child processes, and return it.
742 */
743static struct fsck_instance *wait_one(int flags)
744{
1bf9e264 745 int status = 0;
607c2a72
KZ
746 int sig;
747 struct fsck_instance *inst, *inst2, *prev;
748 pid_t pid;
5a0da00a 749 struct rusage rusage;
607c2a72
KZ
750
751 if (!instance_list)
752 return NULL;
753
754 if (noexecute) {
755 inst = instance_list;
87918040 756 prev = NULL;
607c2a72
KZ
757#ifdef RANDOM_DEBUG
758 while (inst->next && (random() & 1)) {
759 prev = inst;
760 inst = inst->next;
761 }
762#endif
763 inst->exit_status = 0;
764 goto ret_inst;
765 }
766
767 /*
768 * gcc -Wall fails saving throw against stupidity
769 * (inst and prev are thought to be uninitialized variables)
770 */
771 inst = prev = NULL;
772
773 do {
5a0da00a 774 pid = wait4(-1, &status, flags, &rusage);
607c2a72
KZ
775 if (cancel_requested && !kill_sent) {
776 kill_all(SIGTERM);
777 kill_sent++;
778 }
779 if ((pid == 0) && (flags & WNOHANG))
780 return NULL;
781 if (pid < 0) {
782 if ((errno == EINTR) || (errno == EAGAIN))
783 continue;
784 if (errno == ECHILD) {
0a09eb4e 785 warnx(_("wait: no more child process?!?"));
607c2a72
KZ
786 return NULL;
787 }
83217641 788 warn(_("waitpid failed"));
607c2a72
KZ
789 continue;
790 }
87918040 791 for (prev = NULL, inst = instance_list;
607c2a72
KZ
792 inst;
793 prev = inst, inst = inst->next) {
794 if (inst->pid == pid)
795 break;
796 }
797 } while (!inst);
798
799 if (WIFEXITED(status))
800 status = WEXITSTATUS(status);
801 else if (WIFSIGNALED(status)) {
802 sig = WTERMSIG(status);
803 if (sig == SIGINT) {
947558f5 804 status = FSCK_EX_UNCORRECTED;
607c2a72 805 } else {
0a09eb4e
SK
806 warnx(_("Warning... %s for device %s exited "
807 "with signal %d."),
2b505124 808 inst->prog, fs_get_device(inst->fs), sig);
947558f5 809 status = FSCK_EX_ERROR;
607c2a72
KZ
810 }
811 } else {
0a09eb4e 812 warnx(_("%s %s: status is %x, should never happen."),
2b505124 813 inst->prog, fs_get_device(inst->fs), status);
947558f5 814 status = FSCK_EX_ERROR;
607c2a72 815 }
5a0da00a 816
607c2a72
KZ
817 inst->exit_status = status;
818 inst->flags |= FLAG_DONE;
c9e24d60 819 gettime_monotonic(&inst->end_time);
5a0da00a
FM
820 memcpy(&inst->rusage, &rusage, sizeof(struct rusage));
821
607c2a72
KZ
822 if (progress && (inst->flags & FLAG_PROGRESS) &&
823 !progress_active()) {
824 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
825 if (inst2->flags & FLAG_DONE)
826 continue;
ad296391 827 if (strcmp(inst2->type, "ext2") != 0 &&
607c2a72 828 strcmp(inst2->type, "ext3") &&
ad296391
RP
829 strcmp(inst2->type, "ext4") != 0 &&
830 strcmp(inst2->type, "ext4dev") != 0)
607c2a72
KZ
831 continue;
832 /*
833 * If we've just started the fsck, wait a tiny
834 * bit before sending the kill, to give it
835 * time to set up the signal handler
836 */
87918040 837 if (inst2->start_time.tv_sec < time(NULL) + 2) {
607c2a72
KZ
838 if (fork() == 0) {
839 sleep(1);
840 kill(inst2->pid, SIGUSR1);
947558f5 841 exit(FSCK_EX_OK);
607c2a72
KZ
842 }
843 } else
844 kill(inst2->pid, SIGUSR1);
845 inst2->flags |= FLAG_PROGRESS;
846 break;
847 }
848 }
849ret_inst:
850 if (prev)
851 prev->next = inst->next;
852 else
853 instance_list = inst->next;
5a0da00a
FM
854
855 print_stats(inst);
856
607c2a72
KZ
857 if (verbose > 1)
858 printf(_("Finished with %s (exit status %d)\n"),
2b505124 859 fs_get_device(inst->fs), inst->exit_status);
607c2a72
KZ
860 num_running--;
861 return inst;
862}
863
864#define FLAG_WAIT_ALL 0
865#define FLAG_WAIT_ATLEAST_ONE 1
866/*
867 * Wait until all executing child processes have exited; return the
868 * logical OR of all of their exit code values.
869 */
870static int wait_many(int flags)
871{
872 struct fsck_instance *inst;
873 int global_status = 0;
874 int wait_flags = 0;
875
876 while ((inst = wait_one(wait_flags))) {
877 global_status |= inst->exit_status;
878 free_instance(inst);
879#ifdef RANDOM_DEBUG
880 if (noexecute && (flags & WNOHANG) && !(random() % 3))
881 break;
882#endif
883 if (flags & FLAG_WAIT_ATLEAST_ONE)
884 wait_flags = WNOHANG;
885 }
886 return global_status;
887}
888
889/*
890 * Run the fsck program on a particular device
891 *
892 * If the type is specified using -t, and it isn't prefixed with "no"
893 * (as in "noext2") and only one filesystem type is specified, then
894 * use that type regardless of what is specified in /etc/fstab.
895 *
896 * If the type isn't specified by the user, then use either the type
897 * specified in /etc/fstab, or DEFAULT_FSTYPE.
898 */
2b505124 899static int fsck_device(struct libmnt_fs *fs, int interactive)
607c2a72 900{
8bad4fc0 901 char *progname, *progpath;
607c2a72
KZ
902 const char *type;
903 int retval;
904
2b505124
KZ
905 fs_interpret_type(fs);
906
907 type = mnt_fs_get_fstype(fs);
607c2a72 908
2b505124
KZ
909 if (type && strcmp(type, "auto") != 0)
910 ;
ad296391
RP
911 else if (fstype && strncmp(fstype, "no", 2) != 0 &&
912 strncmp(fstype, "opts=", 5) != 0 && strncmp(fstype, "loop", 4) != 0 &&
607c2a72
KZ
913 !strchr(fstype, ','))
914 type = fstype;
915 else
916 type = DEFAULT_FSTYPE;
917
8bad4fc0 918 xasprintf(&progname, "fsck.%s", type);
ac528367
KZ
919
920 if (!find_fsck(progname, &progpath)) {
8bad4fc0 921 free(progname);
c7a96884
KZ
922 if (fs_check_required(type)) {
923 retval = ENOENT;
924 goto err;
925 }
926 return 0;
927 }
928
607c2a72 929 num_running++;
c7a96884 930 retval = execute(progname, progpath, type, fs, interactive);
8bad4fc0
TT
931 free(progname);
932 free(progpath);
607c2a72 933 if (retval) {
607c2a72 934 num_running--;
c7a96884 935 goto err;
607c2a72 936 }
6c6f2af9 937 return 0;
c7a96884 938err:
7f9e76b4
SK
939 warnx(_("error %d (%s) while executing fsck.%s for %s"),
940 retval, strerror(errno), type, fs_get_device(fs));
c7a96884 941 return FSCK_EX_ERROR;
607c2a72
KZ
942}
943
944
945/*
946 * Deal with the fsck -t argument.
947 */
2ba641e5 948static struct fs_type_compile {
607c2a72
KZ
949 char **list;
950 int *type;
951 int negate;
952} fs_type_compiled;
953
954#define FS_TYPE_NORMAL 0
955#define FS_TYPE_OPT 1
956#define FS_TYPE_NEGOPT 2
957
607c2a72
KZ
958static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
959{
5894bcd4 960 char *cp, *list, *s;
607c2a72
KZ
961 int num = 2;
962 int negate, first_negate = 1;
963
964 if (fs_type) {
965 for (cp=fs_type; *cp; cp++) {
966 if (*cp == ',')
967 num++;
968 }
969 }
970
ac8f2843
KZ
971 cmp->list = xcalloc(num, sizeof(char *));
972 cmp->type = xcalloc(num, sizeof(int));
607c2a72
KZ
973 cmp->negate = 0;
974
975 if (!fs_type)
976 return;
977
2a24e16e 978 list = xstrdup(fs_type);
607c2a72
KZ
979 num = 0;
980 s = strtok(list, ",");
981 while(s) {
982 negate = 0;
983 if (strncmp(s, "no", 2) == 0) {
984 s += 2;
985 negate = 1;
986 } else if (*s == '!') {
987 s++;
988 negate = 1;
989 }
990 if (strcmp(s, "loop") == 0)
991 /* loop is really short-hand for opts=loop */
992 goto loop_special_case;
993 else if (strncmp(s, "opts=", 5) == 0) {
994 s += 5;
995 loop_special_case:
996 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
997 } else {
998 if (first_negate) {
999 cmp->negate = negate;
1000 first_negate = 0;
1001 }
1002 if ((negate && !cmp->negate) ||
1003 (!negate && cmp->negate)) {
947558f5 1004 errx(FSCK_EX_USAGE,
e0eadc0d
SK
1005 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
1006 "with 'no' or '!'."));
607c2a72
KZ
1007 }
1008 }
ac8f2843 1009
2a24e16e 1010 cmp->list[num++] = xstrdup(s);
607c2a72
KZ
1011 s = strtok(NULL, ",");
1012 }
1013 free(list);
1014}
1015
1016/*
1017 * This function returns true if a particular option appears in a
1018 * comma-delimited options list
1019 */
2b505124 1020static int opt_in_list(const char *opt, const char *optlist)
607c2a72
KZ
1021{
1022 char *list, *s;
1023
1024 if (!optlist)
1025 return 0;
2a24e16e 1026 list = xstrdup(optlist);
607c2a72
KZ
1027
1028 s = strtok(list, ",");
1029 while(s) {
1030 if (strcmp(s, opt) == 0) {
1031 free(list);
1032 return 1;
1033 }
1034 s = strtok(NULL, ",");
1035 }
0a09eb4e 1036 free(list);
607c2a72
KZ
1037 return 0;
1038}
1039
1040/* See if the filesystem matches the criteria given by the -t option */
2b505124 1041static int fs_match(struct libmnt_fs *fs, struct fs_type_compile *cmp)
607c2a72
KZ
1042{
1043 int n, ret = 0, checked_type = 0;
1044 char *cp;
1045
87918040 1046 if (cmp->list == NULL || cmp->list[0] == NULL)
607c2a72
KZ
1047 return 1;
1048
1049 for (n=0; (cp = cmp->list[n]); n++) {
1050 switch (cmp->type[n]) {
1051 case FS_TYPE_NORMAL:
2b505124
KZ
1052 {
1053 const char *type = mnt_fs_get_fstype(fs);
1054
607c2a72 1055 checked_type++;
2b505124 1056 if (type && strcmp(cp, type) == 0)
607c2a72 1057 ret = 1;
607c2a72 1058 break;
2b505124 1059 }
607c2a72 1060 case FS_TYPE_NEGOPT:
2b505124 1061 if (opt_in_list(cp, mnt_fs_get_options(fs)))
607c2a72
KZ
1062 return 0;
1063 break;
1064 case FS_TYPE_OPT:
2b505124 1065 if (!opt_in_list(cp, mnt_fs_get_options(fs)))
607c2a72
KZ
1066 return 0;
1067 break;
1068 }
1069 }
1070 if (checked_type == 0)
1071 return 1;
1072 return (cmp->negate ? !ret : ret);
1073}
1074
1bb516c3
LN
1075/*
1076 * Check if a device exists
1077 */
1078static int device_exists(const char *device)
1079{
1080 struct stat st;
1081
1082 if (stat(device, &st) == -1)
1083 return 0;
1bb516c3
LN
1084 if (!S_ISBLK(st.st_mode))
1085 return 0;
1bb516c3
LN
1086 return 1;
1087}
1088
2b505124 1089static int fs_ignored_type(struct libmnt_fs *fs)
e3174198 1090{
2b505124
KZ
1091 const char **ip, *type;
1092
868cd0dc 1093 if (!mnt_fs_is_regularfs(fs))
2b505124
KZ
1094 return 1;
1095
1096 type = mnt_fs_get_fstype(fs);
e3174198 1097
2b505124
KZ
1098 for(ip = ignored_types; type && *ip; ip++) {
1099 if (strcmp(type, *ip) == 0)
e3174198
KZ
1100 return 1;
1101 }
1102
1103 return 0;
1104}
1105
607c2a72 1106/* Check if we should ignore this filesystem. */
2b505124 1107static int ignore(struct libmnt_fs *fs)
607c2a72 1108{
c7a96884 1109 const char *type;
607c2a72
KZ
1110
1111 /*
1112 * If the pass number is 0, ignore it.
1113 */
2b505124 1114 if (mnt_fs_get_passno(fs) == 0)
607c2a72
KZ
1115 return 1;
1116
1117 /*
1118 * If this is a bind mount, ignore it.
1119 */
2b505124 1120 if (opt_in_list("bind", mnt_fs_get_options(fs))) {
0a09eb4e
SK
1121 warnx(_("%s: skipping bad line in /etc/fstab: "
1122 "bind mount with nonzero fsck pass number"),
2b505124 1123 mnt_fs_get_target(fs));
607c2a72
KZ
1124 return 1;
1125 }
1126
1bb516c3
LN
1127 /*
1128 * ignore devices that don't exist and have the "nofail" mount option
1129 */
2b505124
KZ
1130 if (!device_exists(fs_get_device(fs))) {
1131 if (opt_in_list("nofail", mnt_fs_get_options(fs))) {
236acf2d
KZ
1132 if (verbose)
1133 printf(_("%s: skipping nonexistent device\n"),
2b505124 1134 fs_get_device(fs));
236acf2d
KZ
1135 return 1;
1136 }
1bb516c3 1137 if (verbose)
236acf2d
KZ
1138 printf(_("%s: nonexistent device (\"nofail\" fstab "
1139 "option may be used to skip this device)\n"),
2b505124 1140 fs_get_device(fs));
1bb516c3
LN
1141 }
1142
2b505124 1143 fs_interpret_type(fs);
607c2a72
KZ
1144
1145 /*
1146 * If a specific fstype is specified, and it doesn't match,
1147 * ignore it.
1148 */
3d4820f4
KZ
1149 if (!fs_match(fs, &fs_type_compiled))
1150 return 1;
607c2a72 1151
2b505124 1152 type = mnt_fs_get_fstype(fs);
7064679f
KZ
1153 if (!type) {
1154 if (verbose)
1155 printf(_("%s: skipping unknown filesystem type\n"),
1156 fs_get_device(fs));
1157 return 1;
1158 }
2b505124 1159
607c2a72 1160 /* Are we ignoring this type? */
2b505124 1161 if (fs_ignored_type(fs))
e3174198 1162 return 1;
607c2a72 1163
c7a96884 1164
607c2a72
KZ
1165
1166 /* See if the <fsck.fs> program is available. */
ac528367 1167 if (!find_fsck(type, NULL)) {
c7a96884 1168 if (fs_check_required(type))
0a09eb4e 1169 warnx(_("cannot check %s: fsck.%s not found"),
2b505124 1170 fs_get_device(fs), type);
607c2a72
KZ
1171 return 1;
1172 }
1173
1174 /* We can and want to check this file system type. */
1175 return 0;
1176}
1177
0c0f93fc
KZ
1178static int count_slaves(dev_t disk)
1179{
1180 DIR *dir;
1181 struct dirent *dp;
1182 char dirname[PATH_MAX];
1183 int count = 0;
1184
1185 snprintf(dirname, sizeof(dirname),
1186 "/sys/dev/block/%u:%u/slaves/",
1187 major(disk), minor(disk));
1188
1189 if (!(dir = opendir(dirname)))
1190 return -1;
1191
87918040 1192 while ((dp = readdir(dir)) != NULL) {
0c0f93fc
KZ
1193#ifdef _DIRENT_HAVE_D_TYPE
1194 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
1195 continue;
1196#endif
1197 if (dp->d_name[0] == '.' &&
1198 ((dp->d_name[1] == 0) ||
1199 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
1200 continue;
1201
1202 count++;
1203 }
ac8f2843 1204
0c0f93fc
KZ
1205 closedir(dir);
1206 return count;
1207}
1208
607c2a72
KZ
1209/*
1210 * Returns TRUE if a partition on the same disk is already being
1211 * checked.
1212 */
2b505124 1213static int disk_already_active(struct libmnt_fs *fs)
607c2a72
KZ
1214{
1215 struct fsck_instance *inst;
2b505124 1216 dev_t disk;
607c2a72
KZ
1217
1218 if (force_all_parallel)
1219 return 0;
1220
2b505124 1221 if (instance_list && fs_is_stacked(instance_list->fs))
0c0f93fc 1222 /* any instance for a stacked device is already running */
607c2a72 1223 return 1;
607c2a72 1224
2b505124 1225 disk = fs_get_disk(fs, 1);
0c0f93fc 1226
607c2a72
KZ
1227 /*
1228 * If we don't know the base device, assume that the device is
1229 * already active if there are any fsck instances running.
0c0f93fc
KZ
1230 *
1231 * Don't check a stacked device with any other disk too.
607c2a72 1232 */
2b505124 1233 if (!disk || fs_is_stacked(fs))
87918040 1234 return (instance_list != NULL);
0c0f93fc 1235
607c2a72 1236 for (inst = instance_list; inst; inst = inst->next) {
2b505124
KZ
1237 dev_t idisk = fs_get_disk(inst->fs, 0);
1238
1239 if (!idisk || disk == idisk)
607c2a72 1240 return 1;
607c2a72 1241 }
ac8f2843 1242
607c2a72
KZ
1243 return 0;
1244}
1245
1246/* Check all file systems, using the /etc/fstab table. */
2b505124 1247static int check_all(void)
607c2a72 1248{
607c2a72
KZ
1249 int not_done_yet = 1;
1250 int passno = 1;
1251 int pass_done;
2b505124 1252 int status = FSCK_EX_OK;
607c2a72 1253
2b505124
KZ
1254 struct libmnt_fs *fs;
1255 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
1256
1257 if (!itr)
1258 err(FSCK_EX_ERROR, _("failed to allocate iterator"));
607c2a72
KZ
1259
1260 /*
1261 * Do an initial scan over the filesystem; mark filesystems
1262 * which should be ignored as done, and resolve any "auto"
1263 * filesystem types (done as a side-effect of calling ignore()).
1264 */
2b505124
KZ
1265 while (mnt_table_next_fs(fstab, itr, &fs) == 0) {
1266 if (ignore(fs)) {
1267 fs_set_done(fs);
1268 continue;
1269 }
607c2a72
KZ
1270 }
1271
2b505124
KZ
1272 if (verbose)
1273 fputs(_("Checking all file systems.\n"), stdout);
1274
607c2a72
KZ
1275 /*
1276 * Find and check the root filesystem.
1277 */
1278 if (!parallel_root) {
2b505124 1279 fs = mnt_table_find_target(fstab, "/", MNT_ITER_FORWARD);
607c2a72 1280 if (fs) {
2b505124
KZ
1281 if (!skip_root &&
1282 !fs_is_done(fs) &&
67f09eae 1283 !(ignore_mounted && is_mounted(fs))) {
6c6f2af9 1284 status |= fsck_device(fs, 1);
607c2a72 1285 status |= wait_many(FLAG_WAIT_ALL);
2b505124
KZ
1286 if (status > FSCK_EX_NONDESTRUCT) {
1287 mnt_free_iter(itr);
607c2a72 1288 return status;
2b505124 1289 }
607c2a72 1290 }
2b505124 1291 fs_set_done(fs);
607c2a72
KZ
1292 }
1293 }
2b505124 1294
607c2a72
KZ
1295 /*
1296 * This is for the bone-headed user who enters the root
9e930041 1297 * filesystem twice. Skip root will skip all root entries.
607c2a72 1298 */
2b505124
KZ
1299 if (skip_root) {
1300 mnt_reset_iter(itr, MNT_ITER_FORWARD);
1301
1302 while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1303 const char *tgt = mnt_fs_get_target(fs);
1304
1305 if (tgt && strcmp(tgt, "/") == 0)
1306 fs_set_done(fs);
1307 }
1308 }
607c2a72
KZ
1309
1310 while (not_done_yet) {
1311 not_done_yet = 0;
1312 pass_done = 1;
1313
2b505124
KZ
1314 mnt_reset_iter(itr, MNT_ITER_FORWARD);
1315
1316 while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1317
607c2a72
KZ
1318 if (cancel_requested)
1319 break;
2b505124 1320 if (fs_is_done(fs))
607c2a72
KZ
1321 continue;
1322 /*
1323 * If the filesystem's pass number is higher
1324 * than the current pass number, then we don't
1325 * do it yet.
1326 */
2b505124 1327 if (mnt_fs_get_passno(fs) > passno) {
607c2a72
KZ
1328 not_done_yet++;
1329 continue;
1330 }
67f09eae 1331 if (ignore_mounted && is_mounted(fs)) {
2b505124 1332 fs_set_done(fs);
607c2a72
KZ
1333 continue;
1334 }
1335 /*
1336 * If a filesystem on a particular device has
1337 * already been spawned, then we need to defer
1338 * this to another pass.
1339 */
0c0f93fc 1340 if (disk_already_active(fs)) {
607c2a72
KZ
1341 pass_done = 0;
1342 continue;
1343 }
1344 /*
1345 * Spawn off the fsck process
1346 */
6c6f2af9 1347 status |= fsck_device(fs, serialize);
2b505124 1348 fs_set_done(fs);
607c2a72
KZ
1349
1350 /*
1351 * Only do one filesystem at a time, or if we
1352 * have a limit on the number of fsck's extant
1353 * at one time, apply that limit.
1354 */
1355 if (serialize ||
1356 (max_running && (num_running >= max_running))) {
1357 pass_done = 0;
1358 break;
1359 }
1360 }
1361 if (cancel_requested)
1362 break;
1363 if (verbose > 1)
1364 printf(_("--waiting-- (pass %d)\n"), passno);
2b505124 1365
607c2a72
KZ
1366 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1367 FLAG_WAIT_ATLEAST_ONE);
1368 if (pass_done) {
1369 if (verbose > 1)
1370 printf("----------------------------------\n");
1371 passno++;
1372 } else
1373 not_done_yet++;
1374 }
2b505124 1375
607c2a72
KZ
1376 if (cancel_requested && !kill_sent) {
1377 kill_all(SIGTERM);
1378 kill_sent++;
1379 }
2b505124 1380
607c2a72 1381 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
2b505124 1382 mnt_free_iter(itr);
607c2a72
KZ
1383 return status;
1384}
1385
58745f35 1386static void __attribute__((__noreturn__)) usage(void)
607c2a72 1387{
58745f35 1388 FILE *out = stdout;
1b8ce047
KZ
1389 fputs(USAGE_HEADER, out);
1390 fprintf(out, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1391 program_invocation_short_name);
1392
451dbcfa
BS
1393 fputs(USAGE_SEPARATOR, out);
1394 fputs(_("Check and repair a Linux filesystem.\n"), out);
1395
1b8ce047
KZ
1396 fputs(USAGE_OPTIONS, out);
1397 fputs(_(" -A check all filesystems\n"), out);
1398 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out);
1399 fputs(_(" -l lock the device to guarantee exclusive access\n"), out);
1400 fputs(_(" -M do not check mounted filesystems\n"), out);
1401 fputs(_(" -N do not execute, just show what would be done\n"), out);
1402 fputs(_(" -P check filesystems in parallel, including root\n"), out);
1403 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out);
07c09a29
SB
1404 fputs(_(" -r [<fd>] report statistics for each device checked;\n"
1405 " file descriptor is for GUIs\n"), out);
1b8ce047
KZ
1406 fputs(_(" -s serialize the checking operations\n"), out);
1407 fputs(_(" -T do not show the title on startup\n"), out);
1408 fputs(_(" -t <type> specify filesystem types to be checked;\n"
07c09a29 1409 " <type> is allowed to be a comma-separated list\n"), out);
1b8ce047 1410 fputs(_(" -V explain what is being done\n"), out);
1b8ce047 1411
58745f35 1412 fputs(USAGE_SEPARATOR, out);
b3054454
RM
1413 printf( " -?, --help %s\n", USAGE_OPTSTR_HELP);
1414 printf( " --version %s\n", USAGE_OPTSTR_VERSION);
1b8ce047
KZ
1415 fputs(USAGE_SEPARATOR, out);
1416 fputs(_("See the specific fsck.* commands for available fs-options."), out);
f45f3ec3 1417 printf(USAGE_MAN_TAIL("fsck(8)"));
58745f35 1418 exit(FSCK_EX_OK);
607c2a72
KZ
1419}
1420
7ac166bf 1421static void signal_cancel(int sig __attribute__((__unused__)))
607c2a72
KZ
1422{
1423 cancel_requested++;
1424}
1425
9895daca 1426static void parse_argv(int argc, char *argv[])
607c2a72
KZ
1427{
1428 int i, j;
87918040 1429 char *arg, *dev, *tmp = NULL;
607c2a72
KZ
1430 char options[128];
1431 int opt = 0;
1432 int opts_for_fsck = 0;
1433 struct sigaction sa;
658c0891 1434 int report_stats_fd = -1;
607c2a72
KZ
1435
1436 /*
1437 * Set up signal action
1438 */
1439 memset(&sa, 0, sizeof(struct sigaction));
1440 sa.sa_handler = signal_cancel;
87918040
SK
1441 sigaction(SIGINT, &sa, NULL);
1442 sigaction(SIGTERM, &sa, NULL);
607c2a72
KZ
1443
1444 num_devices = 0;
1445 num_args = 0;
87918040 1446 instance_list = NULL;
607c2a72 1447
607c2a72
KZ
1448 for (i=1; i < argc; i++) {
1449 arg = argv[i];
1450 if (!arg)
1451 continue;
58745f35
RM
1452
1453 /* the only two longopts to satisfy UL standards */
1454 if (!opts_for_fsck && !strcmp(arg, "--help"))
1455 usage();
2c308875
KZ
1456 if (!opts_for_fsck && !strcmp(arg, "--version"))
1457 print_version(FSCK_EX_OK);
58745f35 1458
607c2a72 1459 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
0a09eb4e 1460 if (num_devices >= MAX_DEVICES)
947558f5 1461 errx(FSCK_EX_ERROR, _("too many devices"));
e33b39a8
KZ
1462
1463 dev = mnt_resolve_spec(arg, mntcache);
1464
607c2a72
KZ
1465 if (!dev && strchr(arg, '=')) {
1466 /*
1467 * Check to see if we failed because
1468 * /proc/partitions isn't found.
1469 */
fb429f22 1470 if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) {
289dcc90 1471 warn(_("cannot open %s"),
0a09eb4e 1472 _PATH_PROC_PARTITIONS);
947558f5 1473 errx(FSCK_EX_ERROR, _("Is /proc mounted?"));
607c2a72
KZ
1474 }
1475 /*
1476 * Check to see if this is because
1477 * we're not running as root
1478 */
1479 if (geteuid())
947558f5 1480 errx(FSCK_EX_ERROR,
0a09eb4e
SK
1481 _("must be root to scan for matching filesystems: %s"),
1482 arg);
607c2a72 1483 else
947558f5 1484 errx(FSCK_EX_ERROR,
0a09eb4e
SK
1485 _("couldn't find matching filesystem: %s"),
1486 arg);
607c2a72 1487 }
2a24e16e 1488 devices[num_devices++] = dev ? dev : xstrdup(arg);
607c2a72
KZ
1489 continue;
1490 }
1491 if (arg[0] != '-' || opts_for_fsck) {
0a09eb4e 1492 if (num_args >= MAX_ARGS)
947558f5 1493 errx(FSCK_EX_ERROR, _("too many arguments"));
2a24e16e 1494 args[num_args++] = xstrdup(arg);
607c2a72
KZ
1495 continue;
1496 }
1497 for (j=1; arg[j]; j++) {
1498 if (opts_for_fsck) {
1499 options[++opt] = arg[j];
1500 continue;
1501 }
1502 switch (arg[j]) {
1503 case 'A':
0a09eb4e 1504 doall = 1;
607c2a72
KZ
1505 break;
1506 case 'C':
0a09eb4e 1507 progress = 1;
0b1bb283 1508 if (arg[j+1]) { /* -C<fd> */
607c2a72
KZ
1509 progress_fd = string_to_int(arg+j+1);
1510 if (progress_fd < 0)
1511 progress_fd = 0;
1512 else
1513 goto next_arg;
0b1bb283 1514 } else if (i+1 < argc && *argv[i+1] != '-') { /* -C <fd> */
d1bba731 1515 progress_fd = string_to_int(argv[i+1]);
607c2a72
KZ
1516 if (progress_fd < 0)
1517 progress_fd = 0;
1518 else {
f1c2eaac 1519 ++i;
607c2a72 1520 goto next_arg;
607c2a72
KZ
1521 }
1522 }
1523 break;
dd0bd943 1524 case 'l':
0a09eb4e 1525 lockdisk = 1;
dd0bd943 1526 break;
607c2a72
KZ
1527 case 'V':
1528 verbose++;
1529 break;
1530 case 'N':
0a09eb4e 1531 noexecute = 1;
607c2a72
KZ
1532 break;
1533 case 'R':
0a09eb4e 1534 skip_root = 1;
607c2a72
KZ
1535 break;
1536 case 'T':
0a09eb4e 1537 notitle = 1;
607c2a72
KZ
1538 break;
1539 case 'M':
0a09eb4e 1540 ignore_mounted = 1;
607c2a72
KZ
1541 break;
1542 case 'P':
0a09eb4e 1543 parallel_root = 1;
607c2a72 1544 break;
5a0da00a
FM
1545 case 'r':
1546 report_stats = 1;
07c09a29 1547 if (arg[j+1]) { /* -r<fd> */
62eea9ce 1548 report_stats_fd = strtou32_or_err(arg+j+1, _("invalid argument of -r"));
658c0891 1549 goto next_arg;
d60d5b74 1550 } else if (i+1 < argc && *argv[i+1] >= '0' && *argv[i+1] <= '9') { /* -r <fd> */
62eea9ce 1551 report_stats_fd = strtou32_or_err(argv[i+1], _("invalid argument of -r"));
658c0891
KZ
1552 ++i;
1553 goto next_arg;
07c09a29 1554 }
5a0da00a 1555 break;
607c2a72 1556 case 's':
0a09eb4e 1557 serialize = 1;
607c2a72
KZ
1558 break;
1559 case 't':
87918040 1560 tmp = NULL;
607c2a72 1561 if (fstype)
58745f35
RM
1562 errx(FSCK_EX_USAGE,
1563 _("option '%s' may be specified only once"), "-t");
607c2a72
KZ
1564 if (arg[j+1])
1565 tmp = arg+j+1;
1566 else if ((i+1) < argc)
1567 tmp = argv[++i];
1568 else
58745f35
RM
1569 errx(FSCK_EX_USAGE,
1570 _("option '%s' requires an argument"), "-t");
2a24e16e 1571 fstype = xstrdup(tmp);
607c2a72
KZ
1572 compile_fs_type(fstype, &fs_type_compiled);
1573 goto next_arg;
1574 case '-':
1575 opts_for_fsck++;
1576 break;
1577 case '?':
58745f35 1578 usage();
607c2a72
KZ
1579 break;
1580 default:
1581 options[++opt] = arg[j];
1582 break;
1583 }
1584 }
1585 next_arg:
1586 if (opt) {
1587 options[0] = '-';
1588 options[++opt] = '\0';
0a09eb4e 1589 if (num_args >= MAX_ARGS)
947558f5 1590 errx(FSCK_EX_ERROR, _("too many arguments"));
2a24e16e 1591 args[num_args++] = xstrdup(options);
607c2a72
KZ
1592 opt = 0;
1593 }
1594 }
07c09a29
SB
1595
1596 /* Validate the report stats file descriptor to avoid disasters */
658c0891 1597 if (report_stats_fd >= 0) {
07c09a29
SB
1598 report_stats_file = fdopen(report_stats_fd, "w");
1599 if (!report_stats_file)
1600 err(FSCK_EX_ERROR,
62eea9ce 1601 _("invalid argument of -r: %d"),
07c09a29
SB
1602 report_stats_fd);
1603 }
1604
607c2a72
KZ
1605 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1606 force_all_parallel++;
1607 if ((tmp = getenv("FSCK_MAX_INST")))
1608 max_running = atoi(tmp);
1609}
1610
1611int main(int argc, char *argv[])
1612{
1613 int i, status = 0;
1614 int interactive = 0;
2b505124 1615 struct libmnt_fs *fs;
a03bdbcd 1616 const char *path = getenv("PATH");
607c2a72
KZ
1617
1618 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1619 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1620
1621 setlocale(LC_MESSAGES, "");
1622 setlocale(LC_CTYPE, "");
1623 bindtextdomain(PACKAGE, LOCALEDIR);
1624 textdomain(PACKAGE);
2c308875 1625 close_stdout_atexit();
607c2a72 1626
9c8b9fba 1627 strutils_set_exitcode(FSCK_EX_USAGE);
e33b39a8
KZ
1628 mnt_init_debug(0); /* init libmount debug mask */
1629 mntcache = mnt_new_cache(); /* no fatal error if failed */
1630
9895daca 1631 parse_argv(argc, argv);
607c2a72
KZ
1632
1633 if (!notitle)
e421313d 1634 printf(UTIL_LINUX_VERSION);
607c2a72 1635
2b505124 1636 load_fs_info();
607c2a72 1637
a03bdbcd 1638 fsck_path = xstrdup(path && *path ? path : FSCK_DEFAULT_PATH);
607c2a72
KZ
1639
1640 if ((num_devices == 1) || (serialize))
1641 interactive = 1;
1642
dd0bd943 1643 if (lockdisk && (doall || num_devices > 1)) {
0a09eb4e
SK
1644 warnx(_("the -l option can be used with one "
1645 "device only -- ignore"));
dd0bd943
KZ
1646 lockdisk = 0;
1647 }
1648
607c2a72
KZ
1649 /* If -A was specified ("check all"), do that! */
1650 if (doall)
1651 return check_all();
1652
1653 if (num_devices == 0) {
1654 serialize++;
1655 interactive++;
1656 return check_all();
1657 }
1658 for (i = 0 ; i < num_devices; i++) {
1659 if (cancel_requested) {
1660 if (!kill_sent) {
1661 kill_all(SIGTERM);
1662 kill_sent++;
1663 }
1664 break;
1665 }
1666 fs = lookup(devices[i]);
2b505124
KZ
1667 if (!fs)
1668 fs = add_dummy_fs(devices[i]);
1669 else if (fs_ignored_type(fs))
e3174198 1670 continue;
67f09eae 1671 if (ignore_mounted && is_mounted(fs))
607c2a72 1672 continue;
6c6f2af9 1673 status |= fsck_device(fs, interactive);
607c2a72
KZ
1674 if (serialize ||
1675 (max_running && (num_running >= max_running))) {
1676 struct fsck_instance *inst;
1677
1678 inst = wait_one(0);
1679 if (inst) {
1680 status |= inst->exit_status;
1681 free_instance(inst);
1682 }
1683 if (verbose > 1)
1684 printf("----------------------------------\n");
1685 }
1686 }
1687 status |= wait_many(FLAG_WAIT_ALL);
1688 free(fsck_path);
6195f9e6 1689 mnt_unref_cache(mntcache);
50fccba1
KZ
1690 mnt_unref_table(fstab);
1691 mnt_unref_table(mtab);
607c2a72
KZ
1692 return status;
1693}