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