blob: 447b4d0086a72cce4cde91781e436af84c23e1bc [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * fsck --- A generic, parallelizing front-end for the fsck program.
4 * It will automatically try to run fsck programs in parallel if the
5 * devices are on separate spindles. It is based on the same ideas as
6 * the generic front end for fsck by David Engel and Fred van Kempen,
7 * but it has been completely rewritten from scratch to support
8 * parallel execution.
9 *
10 * Written by Theodore Ts'o, <tytso@mit.edu>
11 *
12 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
13 * o Changed -t fstype to behave like with mount when -A (all file
14 * systems) or -M (like mount) is specified.
15 * o fsck looks if it can find the fsck.type program to decide
16 * if it should ignore the fs type. This way more fsck programs
17 * can be added without changing this front-end.
18 * o -R flag skip root file system.
19 *
20 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
21 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
22 *
23 * %Begin-Header%
24 * This file may be redistributed under the terms of the GNU Public
25 * License.
26 * %End-Header%
27 */
28
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000029/* All filesystem specific hooks have been removed.
30 * If filesystem cannot be determined, we will execute
31 * "fsck.auto". Currently this also happens if you specify
32 * UUID=xxx or LABEL=xxx as an object to check.
33 * Detection code for that is also probably has to be in fsck.auto.
34 *
35 * In other words, this is _really_ is just a driver program which
36 * spawns actual fsck.something for each filesystem to check.
37 * It doesn't guess filesystem types from on-disk format.
38 */
39
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000040#include "busybox.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000041
42#define EXIT_OK 0
43#define EXIT_NONDESTRUCT 1
44#define EXIT_DESTRUCT 2
45#define EXIT_UNCORRECTED 4
46#define EXIT_ERROR 8
47#define EXIT_USAGE 16
48#define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
49
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000050/*
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000051 * Internal structure for mount table entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000052 */
53
54struct fs_info {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000055 struct fs_info *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000056 char *device;
57 char *mountpt;
58 char *type;
59 char *opts;
60 int freq;
61 int passno;
62 int flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000063};
64
65#define FLAG_DONE 1
66#define FLAG_PROGRESS 2
67/*
68 * Structure to allow exit codes to be stored
69 */
70struct fsck_instance {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000071 struct fsck_instance *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000072 int pid;
73 int flags;
74 int exit_status;
75 time_t start_time;
76 char *prog;
77 char *type;
78 char *device;
79 char *base_device; /* /dev/hda for /dev/hdaN etc */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000080};
81
Denis Vlasenko0de93752006-12-26 02:51:29 +000082static const char *const ignored_types[] = {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000083 "ignore",
84 "iso9660",
85 "nfs",
86 "proc",
87 "sw",
88 "swap",
89 "tmpfs",
90 "devpts",
91 NULL
92};
93
Denis Vlasenko0de93752006-12-26 02:51:29 +000094#if 0
95static const char *const really_wanted[] = {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000096 "minix",
97 "ext2",
98 "ext3",
99 "jfs",
100 "reiserfs",
101 "xiafs",
102 "xfs",
103 NULL
104};
Denis Vlasenko0de93752006-12-26 02:51:29 +0000105#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000106
107#define BASE_MD "/dev/md"
108
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000109static char **devices;
110static char **args;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000111static int num_devices;
112static int num_args;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000113static int verbose;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000114
115#define FS_TYPE_FLAG_NORMAL 0
116#define FS_TYPE_FLAG_OPT 1
117#define FS_TYPE_FLAG_NEGOPT 2
118static char **fs_type_list;
119static uint8_t *fs_type_flag;
120static smallint fs_type_negated;
121
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000122static volatile smallint cancel_requested;
123static smallint doall;
124static smallint noexecute;
125static smallint serialize;
126static smallint skip_root;
127/* static smallint like_mount; */
128static smallint notitle;
129static smallint parallel_root;
130static smallint force_all_parallel;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000131
132/* "progress indicator" code is somewhat buggy and ext[23] specific.
133 * We should be filesystem agnostic. IOW: there should be a well-defined
134 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
135#define DO_PROGRESS_INDICATOR 0
136#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000137static smallint progress;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000138static int progress_fd;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000139#endif
140
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000141static int num_running;
142static int max_running;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000143static char *fstype;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000144static struct fs_info *filesys_info;
145static struct fs_info *filesys_last;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000146static struct fsck_instance *instance_list;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000147
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000148/*
149 * Return the "base device" given a particular device; this is used to
150 * assure that we only fsck one partition on a particular drive at any
151 * one time. Otherwise, the disk heads will be seeking all over the
152 * place. If the base device cannot be determined, return NULL.
153 *
154 * The base_device() function returns an allocated string which must
155 * be freed.
156 */
157#if ENABLE_FEATURE_DEVFS
158/*
159 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
160 * pathames.
161 */
162static const char *const devfs_hier[] = {
163 "host", "bus", "target", "lun", NULL
164};
165#endif
166
167static char *base_device(const char *device)
168{
169 char *str, *cp;
170#if ENABLE_FEATURE_DEVFS
171 const char *const *hier;
172 const char *disk;
173 int len;
174#endif
175 cp = str = xstrdup(device);
176
177 /* Skip over /dev/; if it's not present, give up. */
178 if (strncmp(cp, "/dev/", 5) != 0)
179 goto errout;
180 cp += 5;
181
182 /*
183 * For md devices, we treat them all as if they were all
184 * on one disk, since we don't know how to parallelize them.
185 */
186 if (cp[0] == 'm' && cp[1] == 'd') {
187 cp[2] = 0;
188 return str;
189 }
190
191 /* Handle DAC 960 devices */
192 if (strncmp(cp, "rd/", 3) == 0) {
193 cp += 3;
194 if (cp[0] != 'c' || !isdigit(cp[1])
195 || cp[2] != 'd' || !isdigit(cp[3]))
196 goto errout;
197 cp[4] = 0;
198 return str;
199 }
200
201 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
202 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
203 cp += 2;
204 /* If there's a single number after /dev/hd, skip it */
205 if (isdigit(*cp))
206 cp++;
207 /* What follows must be an alpha char, or give up */
208 if (!isalpha(*cp))
209 goto errout;
210 cp[1] = 0;
211 return str;
212 }
213
214#if ENABLE_FEATURE_DEVFS
215 /* Now let's handle devfs (ugh) names */
216 len = 0;
217 if (strncmp(cp, "ide/", 4) == 0)
218 len = 4;
219 if (strncmp(cp, "scsi/", 5) == 0)
220 len = 5;
221 if (len) {
222 cp += len;
223 /*
224 * Now we proceed down the expected devfs hierarchy.
225 * i.e., .../host1/bus2/target3/lun4/...
226 * If we don't find the expected token, followed by
227 * some number of digits at each level, abort.
228 */
229 for (hier = devfs_hier; *hier; hier++) {
230 len = strlen(*hier);
231 if (strncmp(cp, *hier, len) != 0)
232 goto errout;
233 cp += len;
234 while (*cp != '/' && *cp != 0) {
235 if (!isdigit(*cp))
236 goto errout;
237 cp++;
238 }
239 cp++;
240 }
241 cp[-1] = 0;
242 return str;
243 }
244
245 /* Now handle devfs /dev/disc or /dev/disk names */
246 disk = 0;
247 if (strncmp(cp, "discs/", 6) == 0)
248 disk = "disc";
249 else if (strncmp(cp, "disks/", 6) == 0)
250 disk = "disk";
251 if (disk) {
252 cp += 6;
253 if (strncmp(cp, disk, 4) != 0)
254 goto errout;
255 cp += 4;
256 while (*cp != '/' && *cp != 0) {
257 if (!isdigit(*cp))
258 goto errout;
259 cp++;
260 }
261 *cp = 0;
262 return str;
263 }
264#endif
265 errout:
266 free(str);
267 return NULL;
268}
269
270static void free_instance(struct fsck_instance *p)
271{
272 free(p->prog);
273 free(p->device);
274 free(p->base_device);
275 free(p);
276}
277
278static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
279 const char *type, const char *opts,
280 int freq, int passno)
281{
282 struct fs_info *fs;
283
284 fs = xzalloc(sizeof(*fs));
285 fs->device = xstrdup(device);
286 fs->mountpt = xstrdup(mntpnt);
287 fs->type = xstrdup(type);
288 fs->opts = xstrdup(opts ? opts : "");
289 fs->freq = freq;
290 fs->passno = passno;
291 /*fs->flags = 0; */
292 /*fs->next = NULL; */
293
294 if (!filesys_info)
295 filesys_info = fs;
296 else
297 filesys_last->next = fs;
298 filesys_last = fs;
299
300 return fs;
301}
302
303static void strip_line(char *line)
304{
305 char *p = line + strlen(line) - 1;
306
307 while (*line) {
308 if (*p != '\n' && *p != '\r')
309 break;
310 *p-- = '\0';
311 }
312}
313
314static char *parse_word(char **buf)
315{
316 char *word, *next;
317
318 word = *buf;
319 if (*word == '\0')
320 return NULL;
321
322 word = skip_whitespace(word);
323 next = skip_non_whitespace(word);
324 if (*next)
325 *next++ = '\0';
326 *buf = next;
327 return word;
328}
329
330static void parse_escape(char *word)
331{
332 char *q, c;
333 const char *p;
334
335 if (!word)
336 return;
337
338 for (p = q = word; *p; q++) {
339 c = *p++;
340 if (c != '\\') {
341 *q = c;
342 } else {
343 *q = bb_process_escape_sequence(&p);
344 }
345 }
346 *q = '\0';
347}
348
349static int parse_fstab_line(char *line, struct fs_info **ret_fs)
350{
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000351 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
352 struct fs_info *fs;
353
354 *ret_fs = 0;
355 strip_line(line);
356 cp = strchr(line, '#');
357 if (cp)
358 *cp = '\0'; /* Ignore everything after the comment char */
359 cp = line;
360
361 device = parse_word(&cp);
362 if (!device) return 0; /* Allow blank lines */
363 mntpnt = parse_word(&cp);
364 type = parse_word(&cp);
365 opts = parse_word(&cp);
366 freq = parse_word(&cp);
367 passno = parse_word(&cp);
368
369 if (!mntpnt || !type)
370 return -1;
371
372 parse_escape(device);
373 parse_escape(mntpnt);
374 parse_escape(type);
375 parse_escape(opts);
376 parse_escape(freq);
377 parse_escape(passno);
378
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000379 if (strchr(type, ','))
380 type = NULL;
381
382 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000383 freq ? atoi(freq) : -1,
384 passno ? atoi(passno) : -1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000385 *ret_fs = fs;
386 return 0;
387}
388
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000389/* Load the filesystem database from /etc/fstab */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000390static void load_fs_info(const char *filename)
391{
392 FILE *f;
393 int lineno = 0;
394 int old_fstab = 1;
395 struct fs_info *fs;
396
397 f = fopen_or_warn(filename, "r");
398 if (f == NULL) {
399 /*bb_perror_msg("WARNING: cannot open %s", filename);*/
400 return;
401 }
402 while (1) {
403 int r;
404 char *buf = xmalloc_getline(f);
405 if (!buf) break;
406 r = parse_fstab_line(buf, &fs);
407 free(buf);
408 lineno++;
409 if (r < 0) {
410 bb_error_msg("WARNING: bad format "
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000411 "on line %d of %s", lineno, filename);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000412 continue;
413 }
414 if (!fs)
415 continue;
416 if (fs->passno < 0)
417 fs->passno = 0;
418 else
419 old_fstab = 0;
420 }
421 fclose(f);
422
423 if (old_fstab) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000424 fputs("\007"
425"WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
426"I will kludge around things for you, but you should fix\n"
427"your /etc/fstab file as soon as you can.\n\n", stderr);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000428 for (fs = filesys_info; fs; fs = fs->next) {
429 fs->passno = 1;
430 }
431 }
432}
433
434/* Lookup filesys in /etc/fstab and return the corresponding entry. */
435static struct fs_info *lookup(char *filesys)
436{
437 struct fs_info *fs;
438
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000439 for (fs = filesys_info; fs; fs = fs->next) {
440 if (strcmp(filesys, fs->device) == 0
441 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
442 )
443 break;
444 }
445
446 return fs;
447}
448
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000449#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000450static int progress_active(void)
451{
452 struct fsck_instance *inst;
453
454 for (inst = instance_list; inst; inst = inst->next) {
455 if (inst->flags & FLAG_DONE)
456 continue;
457 if (inst->flags & FLAG_PROGRESS)
458 return 1;
459 }
460 return 0;
461}
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000462#endif
463
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000464
465/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000466 * Send a signal to all outstanding fsck child processes
467 */
Denis Vlasenko0de93752006-12-26 02:51:29 +0000468static void kill_all_if_cancel_requested(void)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000469{
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000470 static smallint kill_sent;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000471
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000472 struct fsck_instance *inst;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000473
474 if (!cancel_requested || kill_sent)
475 return;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000476
477 for (inst = instance_list; inst; inst = inst->next) {
478 if (inst->flags & FLAG_DONE)
479 continue;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000480 kill(inst->pid, SIGTERM);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000481 }
Denis Vlasenko0de93752006-12-26 02:51:29 +0000482 kill_sent = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000483}
484
485/*
486 * Wait for one child process to exit; when it does, unlink it from
487 * the list of executing child processes, and return it.
488 */
489static struct fsck_instance *wait_one(int flags)
490{
491 int status;
492 int sig;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000493 struct fsck_instance *inst, *prev;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000494 pid_t pid;
495
496 if (!instance_list)
497 return NULL;
498
499 if (noexecute) {
500 inst = instance_list;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000501 prev = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000502#ifdef RANDOM_DEBUG
503 while (inst->next && (random() & 1)) {
504 prev = inst;
505 inst = inst->next;
506 }
507#endif
508 inst->exit_status = 0;
509 goto ret_inst;
510 }
511
512 /*
513 * gcc -Wall fails saving throw against stupidity
514 * (inst and prev are thought to be uninitialized variables)
515 */
516 inst = prev = NULL;
517
518 do {
519 pid = waitpid(-1, &status, flags);
Denis Vlasenko0de93752006-12-26 02:51:29 +0000520 kill_all_if_cancel_requested();
521 if (pid == 0 && (flags & WNOHANG))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000522 return NULL;
523 if (pid < 0) {
Denis Vlasenko0de93752006-12-26 02:51:29 +0000524 if (errno == EINTR || errno == EAGAIN)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000525 continue;
526 if (errno == ECHILD) {
527 bb_error_msg("wait: no more child process?!?");
528 return NULL;
529 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000530 bb_perror_msg("wait");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000531 continue;
532 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000533 prev = NULL;
534 inst = instance_list;
535 while (inst) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000536 if (inst->pid == pid)
537 break;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000538 prev = inst;
539 inst = inst->next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000540 }
541 } while (!inst);
542
543 if (WIFEXITED(status))
544 status = WEXITSTATUS(status);
545 else if (WIFSIGNALED(status)) {
546 sig = WTERMSIG(status);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000547 status = EXIT_UNCORRECTED;
548 if (sig != SIGINT) {
549 printf("Warning... %s %s exited "
550 "with signal %d\n",
551 inst->prog, inst->device, sig);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000552 status = EXIT_ERROR;
553 }
554 } else {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000555 printf("%s %s: status is %x, should never happen\n",
556 inst->prog, inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000557 status = EXIT_ERROR;
558 }
559 inst->exit_status = status;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000560
561#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000562 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000563 struct fsck_instance *inst2;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000564 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
565 if (inst2->flags & FLAG_DONE)
566 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000567 if (strcmp(inst2->type, "ext2") != 0
568 && strcmp(inst2->type, "ext3") != 0
569 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000570 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000571 }
572 /* ext[23], we will send USR1
573 * (request to start displaying progress bar)
574 *
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000575 * If we've just started the fsck, wait a tiny
576 * bit before sending the kill, to give it
577 * time to set up the signal handler
578 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000579 if (inst2->start_time >= time(NULL) - 1)
580 sleep(1);
581 kill(inst2->pid, SIGUSR1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000582 inst2->flags |= FLAG_PROGRESS;
583 break;
584 }
585 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000586#endif
587
Denis Vlasenko0de93752006-12-26 02:51:29 +0000588 ret_inst:
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000589 if (prev)
590 prev->next = inst->next;
591 else
592 instance_list = inst->next;
593 if (verbose > 1)
594 printf("Finished with %s (exit status %d)\n",
595 inst->device, inst->exit_status);
596 num_running--;
597 return inst;
598}
599
600#define FLAG_WAIT_ALL 0
601#define FLAG_WAIT_ATLEAST_ONE 1
602/*
603 * Wait until all executing child processes have exited; return the
604 * logical OR of all of their exit code values.
605 */
606static int wait_many(int flags)
607{
608 struct fsck_instance *inst;
609 int global_status = 0;
610 int wait_flags = 0;
611
612 while ((inst = wait_one(wait_flags))) {
613 global_status |= inst->exit_status;
614 free_instance(inst);
615#ifdef RANDOM_DEBUG
616 if (noexecute && (flags & WNOHANG) && !(random() % 3))
617 break;
618#endif
619 if (flags & FLAG_WAIT_ATLEAST_ONE)
620 wait_flags = WNOHANG;
621 }
622 return global_status;
623}
624
625/*
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000626 * Execute a particular fsck program, and link it into the list of
627 * child processes we are waiting for.
628 */
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000629static void execute(const char *type, const char *device, const char *mntpt,
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000630 int interactive)
631{
632 char *argv[num_args + 4]; /* see count below: */
633 int argc;
634 int i;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000635 struct fsck_instance *inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000636 pid_t pid;
637
638 inst = xzalloc(sizeof(*inst));
639
640 argv[0] = xasprintf("fsck.%s", type); /* 1 */
641 for (i = 0; i < num_args; i++)
642 argv[i+1] = args[i]; /* num_args */
643 argc = num_args + 1;
644
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000645#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000646 if (progress && !progress_active()) {
647 if (strcmp(type, "ext2") == 0
648 || strcmp(type, "ext3") == 0
649 ) {
650 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
651 inst->flags |= FLAG_PROGRESS;
652 }
653 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000654#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000655
656 argv[argc++] = xstrdup(device); /* 1 */
657 argv[argc] = NULL; /* 1 */
658
659 if (verbose || noexecute) {
660 printf("[%s (%d) -- %s]", argv[0], num_running,
661 mntpt ? mntpt : device);
662 for (i = 0; i < argc; i++)
663 printf(" %s", argv[i]);
664 puts("");
665 }
666
667 /* Fork and execute the correct program. */
668 pid = -1;
669 if (!noexecute) {
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000670 pid = spawn(argv);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000671 if (pid < 0)
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000672 bb_perror_msg("%s", argv[0]);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000673 }
674
675 for (i = num_args+1; i < argc; i++)
676 free(argv[i]);
677
678 inst->pid = pid;
679 inst->prog = argv[0];
680 inst->type = xstrdup(type);
681 inst->device = xstrdup(device);
682 inst->base_device = base_device(device);
683 inst->start_time = time(NULL);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000684
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000685 /* Add to the list of running fsck's.
686 * (was adding to the end, but adding to the front is simpler...) */
687 inst->next = instance_list;
688 instance_list = inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000689}
690
691/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000692 * Run the fsck program on a particular device
693 *
694 * If the type is specified using -t, and it isn't prefixed with "no"
695 * (as in "noext2") and only one filesystem type is specified, then
696 * use that type regardless of what is specified in /etc/fstab.
697 *
698 * If the type isn't specified by the user, then use either the type
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000699 * specified in /etc/fstab, or "auto".
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000700 */
701static void fsck_device(struct fs_info *fs, int interactive)
702{
703 const char *type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000704
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000705 if (strcmp(fs->type, "auto") != 0) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000706 type = fs->type;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000707 if (verbose > 2)
708 bb_info_msg("using filesystem type '%s' %s",
709 type, "from fstab");
710 } else if (fstype
Denis Vlasenko0de93752006-12-26 02:51:29 +0000711 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
712 && strncmp(fstype, "opts=", 5) != 0
713 && strncmp(fstype, "loop", 4) != 0
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000714 && !strchr(fstype, ',')
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000715 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000716 type = fstype;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000717 if (verbose > 2)
718 bb_info_msg("using filesystem type '%s' %s",
719 type, "from -t");
720 } else {
721 type = "auto";
722 if (verbose > 2)
723 bb_info_msg("using filesystem type '%s' %s",
724 type, "(default)");
725 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000726
727 num_running++;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000728 execute(type, fs->device, fs->mountpt, interactive);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000729}
730
731/*
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000732 * Returns TRUE if a partition on the same disk is already being
733 * checked.
734 */
735static int device_already_active(char *device)
736{
737 struct fsck_instance *inst;
738 char *base;
739
740 if (force_all_parallel)
741 return 0;
742
743#ifdef BASE_MD
744 /* Don't check a soft raid disk with any other disk */
745 if (instance_list
746 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
747 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
748 ) {
749 return 1;
750 }
751#endif
752
753 base = base_device(device);
754 /*
755 * If we don't know the base device, assume that the device is
756 * already active if there are any fsck instances running.
757 */
758 if (!base)
759 return (instance_list != NULL);
760
761 for (inst = instance_list; inst; inst = inst->next) {
762 if (!inst->base_device || !strcmp(base, inst->base_device)) {
763 free(base);
764 return 1;
765 }
766 }
767
768 free(base);
769 return 0;
770}
771
772/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000773 * This function returns true if a particular option appears in a
774 * comma-delimited options list
775 */
776static int opt_in_list(char *opt, char *optlist)
777{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000778 char *s;
779 int len;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000780
781 if (!optlist)
782 return 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000783
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000784 len = strlen(opt);
785 s = optlist - 1;
786 while (1) {
787 s = strstr(s + 1, opt);
788 if (!s)
789 return 0;
790 /* neither "opt.." nor "xxx,opt.."? */
791 if (s != optlist && s[-1] != ',')
792 continue;
793 /* neither "..opt" nor "..opt,xxx"? */
794 if (s[len] != '\0' && s[len] != ',')
795 continue;
796 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000797 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000798}
799
800/* See if the filesystem matches the criteria given by the -t option */
801static int fs_match(struct fs_info *fs)
802{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000803 int n, ret, checked_type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000804 char *cp;
805
806 if (!fs_type_list)
807 return 1;
808
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000809 ret = 0;
810 checked_type = 0;
811 n = 0;
812 while (1) {
813 cp = fs_type_list[n];
814 if (!cp)
815 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000816 switch (fs_type_flag[n]) {
817 case FS_TYPE_FLAG_NORMAL:
818 checked_type++;
819 if (strcmp(cp, fs->type) == 0)
820 ret = 1;
821 break;
822 case FS_TYPE_FLAG_NEGOPT:
823 if (opt_in_list(cp, fs->opts))
824 return 0;
825 break;
826 case FS_TYPE_FLAG_OPT:
827 if (!opt_in_list(cp, fs->opts))
828 return 0;
829 break;
830 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000831 n++;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000832 }
833 if (checked_type == 0)
834 return 1;
835
836 return (fs_type_negated ? !ret : ret);
837}
838
839/* Check if we should ignore this filesystem. */
840static int ignore(struct fs_info *fs)
841{
842 /*
843 * If the pass number is 0, ignore it.
844 */
845 if (fs->passno == 0)
846 return 1;
847
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000848 /*
849 * If a specific fstype is specified, and it doesn't match,
850 * ignore it.
851 */
852 if (!fs_match(fs))
853 return 1;
854
855 /* Are we ignoring this type? */
856 if (index_in_str_array(ignored_types, fs->type) >= 0)
857 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000858
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000859 /* We can and want to check this file system type. */
860 return 0;
861}
862
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000863/* Check all file systems, using the /etc/fstab table. */
864static int check_all(void)
865{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000866 struct fs_info *fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000867 int status = EXIT_OK;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000868 smallint not_done_yet;
869 smallint pass_done;
870 int passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000871
872 if (verbose)
873 puts("Checking all filesystems");
874
875 /*
876 * Do an initial scan over the filesystem; mark filesystems
877 * which should be ignored as done, and resolve any "auto"
878 * filesystem types (done as a side-effect of calling ignore()).
879 */
880 for (fs = filesys_info; fs; fs = fs->next) {
881 if (ignore(fs))
882 fs->flags |= FLAG_DONE;
883 }
884
885 /*
886 * Find and check the root filesystem.
887 */
888 if (!parallel_root) {
889 for (fs = filesys_info; fs; fs = fs->next) {
890 if (LONE_CHAR(fs->mountpt, '/'))
891 break;
892 }
893 if (fs) {
894 if (!skip_root && !ignore(fs)) {
895 fsck_device(fs, 1);
896 status |= wait_many(FLAG_WAIT_ALL);
897 if (status > EXIT_NONDESTRUCT)
898 return status;
899 }
900 fs->flags |= FLAG_DONE;
901 }
902 }
903 /*
904 * This is for the bone-headed user who enters the root
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000905 * filesystem twice. Skip root will skip all root entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000906 */
907 if (skip_root)
908 for (fs = filesys_info; fs; fs = fs->next)
909 if (LONE_CHAR(fs->mountpt, '/'))
910 fs->flags |= FLAG_DONE;
911
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000912 not_done_yet = 1;
913 passno = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000914 while (not_done_yet) {
915 not_done_yet = 0;
916 pass_done = 1;
917
918 for (fs = filesys_info; fs; fs = fs->next) {
919 if (cancel_requested)
920 break;
921 if (fs->flags & FLAG_DONE)
922 continue;
923 /*
924 * If the filesystem's pass number is higher
925 * than the current pass number, then we don't
926 * do it yet.
927 */
928 if (fs->passno > passno) {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000929 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000930 continue;
931 }
932 /*
933 * If a filesystem on a particular device has
934 * already been spawned, then we need to defer
935 * this to another pass.
936 */
937 if (device_already_active(fs->device)) {
938 pass_done = 0;
939 continue;
940 }
941 /*
942 * Spawn off the fsck process
943 */
944 fsck_device(fs, serialize);
945 fs->flags |= FLAG_DONE;
946
947 /*
948 * Only do one filesystem at a time, or if we
949 * have a limit on the number of fsck's extant
950 * at one time, apply that limit.
951 */
952 if (serialize
953 || (max_running && (num_running >= max_running))
954 ) {
955 pass_done = 0;
956 break;
957 }
958 }
959 if (cancel_requested)
960 break;
961 if (verbose > 1)
962 printf("--waiting-- (pass %d)\n", passno);
963 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
964 FLAG_WAIT_ATLEAST_ONE);
965 if (pass_done) {
966 if (verbose > 1)
967 puts("----------------------------------");
968 passno++;
969 } else
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000970 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000971 }
Denis Vlasenko0de93752006-12-26 02:51:29 +0000972 kill_all_if_cancel_requested();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000973 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
974 return status;
975}
976
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000977/*
978 * Deal with the fsck -t argument.
979 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
980 * Why here we require "-t novfat,nonfs" ??
981 */
982static void compile_fs_type(char *fs_type)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000983{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000984 char *s;
985 int num = 2;
986 smallint negate;
987
988 if (fs_type) {
989 s = fs_type;
990 while ((s = strchr(s, ','))) {
991 num++;
992 s++;
993 }
994 }
995
996 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
997 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
998 fs_type_negated = -1; /* not yet known is it negated or not */
999
1000 if (!fs_type)
1001 return;
1002
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001003 num = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001004 s = fs_type;
1005 while (1) {
1006 char *comma;
1007
1008 negate = 0;
1009 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
1010 s += 2;
1011 negate = 1;
1012 } else if (s[0] == '!') {
1013 s++;
1014 negate = 1;
1015 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +00001016
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001017 if (strcmp(s, "loop") == 0)
1018 /* loop is really short-hand for opts=loop */
1019 goto loop_special_case;
1020 if (strncmp(s, "opts=", 5) == 0) {
1021 s += 5;
1022 loop_special_case:
1023 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
1024 } else {
1025 if (fs_type_negated == -1)
1026 fs_type_negated = negate;
1027 if (fs_type_negated != negate)
1028 bb_error_msg_and_die(
1029"either all or none of the filesystem types passed to -t must be prefixed "
1030"with 'no' or '!'");
1031 }
1032 comma = strchr(s, ',');
1033 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
1034 if (!comma)
1035 break;
1036 s = comma + 1;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001037 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001038}
1039
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001040static void parse_args(int argc, char *argv[])
1041{
1042 int i, j;
1043 char *arg, *tmp;
1044 char *options = NULL;
1045 int optpos = 0;
1046 int opts_for_fsck = 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001047
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001048 /* in bss, so already zeroed
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001049 num_devices = 0;
1050 num_args = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001051 instance_list = NULL;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001052 */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001053
1054/* TODO: getopt32 */
1055 for (i = 1; i < argc; i++) {
1056 arg = argv[i];
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001057
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001058 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001059 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001060// FIXME: must check that arg is a blkdev, or resolve
1061// "/path", "UUID=xxx" or "LABEL=xxx" into block device name
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001062// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001063 devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
1064 devices[num_devices++] = xstrdup(arg);
1065 continue;
1066 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001067
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001068 if (arg[0] != '-' || opts_for_fsck) {
1069 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1070 args[num_args++] = xstrdup(arg);
1071 continue;
1072 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001073
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001074 for (j = 1; arg[j]; j++) {
1075 if (opts_for_fsck) {
1076 optpos++;
1077 /* one extra for '\0' */
1078 options = xrealloc(options, optpos + 2);
1079 options[optpos] = arg[j];
1080 continue;
1081 }
1082 switch (arg[j]) {
1083 case 'A':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001084 doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001085 break;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001086#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001087 case 'C':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001088 progress = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001089 if (arg[++j]) { /* -Cn */
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001090 progress_fd = xatoi_u(&arg[j]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001091 goto next_arg;
1092 }
1093 /* -C n */
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001094 progress_fd = xatoi_u(argv[++i]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001095 goto next_arg;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001096#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001097 case 'V':
1098 verbose++;
1099 break;
1100 case 'N':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001101 noexecute = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001102 break;
1103 case 'R':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001104 skip_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001105 break;
1106 case 'T':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001107 notitle = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001108 break;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001109/* case 'M':
1110 like_mount = 1;
1111 break; */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001112 case 'P':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001113 parallel_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001114 break;
1115 case 's':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001116 serialize = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001117 break;
1118 case 't':
1119 if (fstype)
1120 bb_show_usage();
1121 if (arg[++j])
1122 tmp = &arg[j];
1123 else if (++i < argc)
1124 tmp = argv[i];
1125 else
1126 bb_show_usage();
1127 fstype = xstrdup(tmp);
1128 compile_fs_type(fstype);
1129 goto next_arg;
1130 case '-':
1131 opts_for_fsck++;
1132 break;
1133 case '?':
1134 bb_show_usage();
1135 break;
1136 default:
1137 optpos++;
1138 /* one extra for '\0' */
1139 options = xrealloc(options, optpos + 2);
1140 options[optpos] = arg[j];
1141 break;
1142 }
1143 }
1144 next_arg:
1145 if (optpos) {
1146 options[0] = '-';
1147 options[optpos + 1] = '\0';
1148 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1149 args[num_args++] = options;
1150 optpos = 0;
1151 options = NULL;
1152 }
1153 }
1154 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001155 force_all_parallel = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001156 tmp = getenv("FSCK_MAX_INST");
1157 if (tmp)
1158 max_running = xatoi(tmp);
1159}
1160
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001161static void signal_cancel(int sig ATTRIBUTE_UNUSED)
1162{
1163 cancel_requested = 1;
1164}
1165
Denis Vlasenko06af2162007-02-03 17:28:39 +00001166int fsck_main(int argc, char *argv[]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001167int fsck_main(int argc, char *argv[])
1168{
1169 int i, status = 0;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001170 int interactive;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001171 const char *fstab;
1172 struct fs_info *fs;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001173 struct sigaction sa;
1174
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001175 memset(&sa, 0, sizeof(sa));
1176 sa.sa_handler = signal_cancel;
1177 sigaction(SIGINT, &sa, 0);
1178 sigaction(SIGTERM, &sa, 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001179
1180 setbuf(stdout, NULL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001181
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001182 parse_args(argc, argv);
1183
1184 if (!notitle)
1185 puts("fsck (busybox "BB_VER", "BB_BT")");
1186
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001187 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1188 * so we are scanning it anyway */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001189 fstab = getenv("FSTAB_FILE");
1190 if (!fstab)
1191 fstab = "/etc/fstab";
1192 load_fs_info(fstab);
1193
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001194 interactive = (num_devices == 1) | serialize;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001195
1196 /* If -A was specified ("check all"), do that! */
1197 if (doall)
1198 return check_all();
1199
1200 if (num_devices == 0) {
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001201 serialize = 1;
1202 interactive = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001203 return check_all();
1204 }
1205
Denis Vlasenko0de93752006-12-26 02:51:29 +00001206 for (i = 0; i < num_devices; i++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001207 if (cancel_requested) {
Denis Vlasenko0de93752006-12-26 02:51:29 +00001208 kill_all_if_cancel_requested();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001209 break;
1210 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001211
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001212 fs = lookup(devices[i]);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001213 if (!fs)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001214 fs = create_fs_device(devices[i], 0, "auto", 0, -1, -1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001215 fsck_device(fs, interactive);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001216
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001217 if (serialize
1218 || (max_running && (num_running >= max_running))
1219 ) {
1220 struct fsck_instance *inst;
1221
1222 inst = wait_one(0);
1223 if (inst) {
1224 status |= inst->exit_status;
1225 free_instance(inst);
1226 }
1227 if (verbose > 1)
1228 puts("----------------------------------");
1229 }
1230 }
1231 status |= wait_many(FLAG_WAIT_ALL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001232 return status;
1233}