blob: 4006c9112cb37b0df8f1e29f5b77dfbf9b0c4d52 [file] [log] [blame]
Mike Frysinger51a43b42005-09-24 07:11:16 +00001/*
2 * pfsck --- A generic, parallelizing front-end for the fsck program.
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>
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000010 *
Mike Frysinger51a43b42005-09-24 07:11:16 +000011 * 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 *
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000019 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
Mike Frysinger51a43b42005-09-24 07:11:16 +000021 *
22 * %Begin-Header%
23 * This file may be redistributed under the terms of the GNU Public
24 * License.
25 * %End-Header%
26 */
27
28#include <sys/types.h>
29#include <sys/wait.h>
Mike Frysinger51a43b42005-09-24 07:11:16 +000030#include <sys/stat.h>
31#include <limits.h>
32#include <stdio.h>
33#include <ctype.h>
34#include <string.h>
35#include <time.h>
36#include <stdlib.h>
37#include <errno.h>
38#include <paths.h>
39#include <unistd.h>
40#include <errno.h>
41#include <malloc.h>
42#include <signal.h>
43
44#include "fsck.h"
45#include "blkid/blkid.h"
46
47#include "e2fsbb.h"
48
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +000049#include "busybox.h"
50
Mike Frysinger51a43b42005-09-24 07:11:16 +000051#ifndef _PATH_MNTTAB
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000052#define _PATH_MNTTAB "/etc/fstab"
Mike Frysinger51a43b42005-09-24 07:11:16 +000053#endif
54
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +000055/*
"Vladimir N. Oleynik"3ebb8952005-10-12 12:24:01 +000056 * fsck.h
57 */
58
59#ifndef DEFAULT_FSTYPE
60#define DEFAULT_FSTYPE "ext2"
61#endif
62
63#define MAX_DEVICES 32
64#define MAX_ARGS 32
65
66/*
67 * Internal structure for mount tabel entries.
68 */
69
70struct fs_info {
71 char *device;
72 char *mountpt;
73 char *type;
74 char *opts;
75 int freq;
76 int passno;
77 int flags;
78 struct fs_info *next;
79};
80
81#define FLAG_DONE 1
82#define FLAG_PROGRESS 2
83
84/*
85 * Structure to allow exit codes to be stored
86 */
87struct fsck_instance {
88 int pid;
89 int flags;
90 int exit_status;
91 time_t start_time;
92 char * prog;
93 char * type;
94 char * device;
95 char * base_device;
96 struct fsck_instance *next;
97};
98
99/*
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +0000100 * base_device.c
101 *
102 * Return the "base device" given a particular device; this is used to
103 * assure that we only fsck one partition on a particular drive at any
104 * one time. Otherwise, the disk heads will be seeking all over the
105 * place. If the base device can not be determined, return NULL.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000106 *
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +0000107 * The base_device() function returns an allocated string which must
108 * be freed.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000109 *
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +0000110 */
111
112
113#ifdef CONFIG_FEATURE_DEVFS
114/*
115 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
116 * pathames.
117 */
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000118static const char * const devfs_hier[] = {
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +0000119 "host", "bus", "target", "lun", 0
120};
121#endif
122
123static char *base_device(const char *device)
124{
125 char *str, *cp;
126#ifdef CONFIG_FEATURE_DEVFS
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000127 const char * const *hier;
128 const char *disk;
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +0000129 int len;
130#endif
131
132 cp = str = bb_xstrdup(device);
133
134 /* Skip over /dev/; if it's not present, give up. */
135 if (strncmp(cp, "/dev/", 5) != 0)
136 goto errout;
137 cp += 5;
138
139#if 0 /* this is for old stuff no one uses anymore ? */
140 /* Skip over /dev/dsk/... */
141 if (strncmp(cp, "dsk/", 4) == 0)
142 cp += 4;
143#endif
144
145 /*
146 * For md devices, we treat them all as if they were all
147 * on one disk, since we don't know how to parallelize them.
148 */
149 if (cp[0] == 'm' && cp[1] == 'd') {
150 *(cp+2) = 0;
151 return str;
152 }
153
154 /* Handle DAC 960 devices */
155 if (strncmp(cp, "rd/", 3) == 0) {
156 cp += 3;
157 if (cp[0] != 'c' || cp[2] != 'd' ||
158 !isdigit(cp[1]) || !isdigit(cp[3]))
159 goto errout;
160 *(cp+4) = 0;
161 return str;
162 }
163
164 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
165 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
166 cp += 2;
167 /* If there's a single number after /dev/hd, skip it */
168 if (isdigit(*cp))
169 cp++;
170 /* What follows must be an alpha char, or give up */
171 if (!isalpha(*cp))
172 goto errout;
173 *(cp + 1) = 0;
174 return str;
175 }
176
177#ifdef CONFIG_FEATURE_DEVFS
178 /* Now let's handle devfs (ugh) names */
179 len = 0;
180 if (strncmp(cp, "ide/", 4) == 0)
181 len = 4;
182 if (strncmp(cp, "scsi/", 5) == 0)
183 len = 5;
184 if (len) {
185 cp += len;
186 /*
187 * Now we proceed down the expected devfs hierarchy.
188 * i.e., .../host1/bus2/target3/lun4/...
189 * If we don't find the expected token, followed by
190 * some number of digits at each level, abort.
191 */
192 for (hier = devfs_hier; *hier; hier++) {
193 len = strlen(*hier);
194 if (strncmp(cp, *hier, len) != 0)
195 goto errout;
196 cp += len;
197 while (*cp != '/' && *cp != 0) {
198 if (!isdigit(*cp))
199 goto errout;
200 cp++;
201 }
202 cp++;
203 }
204 *(cp - 1) = 0;
205 return str;
206 }
207
208 /* Now handle devfs /dev/disc or /dev/disk names */
209 disk = 0;
210 if (strncmp(cp, "discs/", 6) == 0)
211 disk = "disc";
212 else if (strncmp(cp, "disks/", 6) == 0)
213 disk = "disk";
214 if (disk) {
215 cp += 6;
216 if (strncmp(cp, disk, 4) != 0)
217 goto errout;
218 cp += 4;
219 while (*cp != '/' && *cp != 0) {
220 if (!isdigit(*cp))
221 goto errout;
222 cp++;
223 }
224 *cp = 0;
225 return str;
226 }
227#endif
228
229errout:
230 free(str);
231 return NULL;
232}
233
234
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000235static const char * const ignored_types[] = {
Mike Frysinger51a43b42005-09-24 07:11:16 +0000236 "ignore",
237 "iso9660",
238 "nfs",
239 "proc",
240 "sw",
241 "swap",
242 "tmpfs",
243 "devpts",
244 NULL
245};
246
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000247static const char * const really_wanted[] = {
Mike Frysinger51a43b42005-09-24 07:11:16 +0000248 "minix",
249 "ext2",
250 "ext3",
251 "jfs",
252 "reiserfs",
253 "xiafs",
254 "xfs",
255 NULL
256};
257
258#define BASE_MD "/dev/md"
259
260/*
261 * Global variables for options
262 */
263static char *devices[MAX_DEVICES];
264static char *args[MAX_ARGS];
265static int num_devices, num_args;
266
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000267static int verbose;
268static int doall;
269static int noexecute;
270static int serialize;
271static int skip_root;
272static int like_mount;
273static int notitle;
274static int parallel_root;
275static int progress;
276static int progress_fd;
277static int force_all_parallel;
278static int num_running;
279static int max_running;
280static volatile int cancel_requested;
281static int kill_sent;
282static char *fstype;
283static struct fs_info *filesys_info, *filesys_last;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000284static struct fsck_instance *instance_list;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000285static char *fsck_path;
286static blkid_cache cache;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000287
288static char *string_copy(const char *s)
289{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000290 char *ret;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000291
292 if (!s)
293 return 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000294 ret = strdup(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000295 return ret;
296}
297
298static int string_to_int(const char *s)
299{
300 long l;
301 char *p;
302
303 l = strtol(s, &p, 0);
304 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
305 return -1;
306 else
307 return (int) l;
308}
309
Mike Frysinger51a43b42005-09-24 07:11:16 +0000310static char *skip_over_blank(char *cp)
311{
312 while (*cp && isspace(*cp))
313 cp++;
314 return cp;
315}
316
317static char *skip_over_word(char *cp)
318{
319 while (*cp && !isspace(*cp))
320 cp++;
321 return cp;
322}
323
324static void strip_line(char *line)
325{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000326 char *p;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000327
328 while (*line) {
329 p = line + strlen(line) - 1;
330 if ((*p == '\n') || (*p == '\r'))
331 *p = 0;
332 else
333 break;
334 }
335}
336
337static char *parse_word(char **buf)
338{
339 char *word, *next;
340
341 word = *buf;
342 if (*word == 0)
343 return 0;
344
345 word = skip_over_blank(word);
346 next = skip_over_word(word);
347 if (*next)
348 *next++ = 0;
349 *buf = next;
350 return word;
351}
352
353static void parse_escape(char *word)
354{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000355 char *q, c;
356 const char *p;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000357
358 if (!word)
359 return;
360
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000361 for (p = q = word; *p; q++) {
362 c = *p++;
363 if (c != '\\') {
364 *q = c;
365 } else {
366 *q = bb_process_escape_sequence(&p);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000367 }
Mike Frysinger51a43b42005-09-24 07:11:16 +0000368 }
369 *q = 0;
370}
371
372static void free_instance(struct fsck_instance *i)
373{
374 if (i->prog)
375 free(i->prog);
376 if (i->device)
377 free(i->device);
378 if (i->base_device)
379 free(i->base_device);
380 free(i);
381 return;
382}
383
384static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000385 const char *type, const char *opts,
Mike Frysinger51a43b42005-09-24 07:11:16 +0000386 int freq, int passno)
387{
388 struct fs_info *fs;
389
390 if (!(fs = malloc(sizeof(struct fs_info))))
391 return NULL;
392
393 fs->device = string_copy(device);
394 fs->mountpt = string_copy(mntpnt);
395 fs->type = string_copy(type);
396 fs->opts = string_copy(opts ? opts : "");
397 fs->freq = freq;
398 fs->passno = passno;
399 fs->flags = 0;
400 fs->next = NULL;
401
402 if (!filesys_info)
403 filesys_info = fs;
404 else
405 filesys_last->next = fs;
406 filesys_last = fs;
407
408 return fs;
409}
410
411
412
413static int parse_fstab_line(char *line, struct fs_info **ret_fs)
414{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000415 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000416 struct fs_info *fs;
417
418 *ret_fs = 0;
419 strip_line(line);
420 if ((cp = strchr(line, '#')))
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000421 *cp = 0; /* Ignore everything after the comment char */
Mike Frysinger51a43b42005-09-24 07:11:16 +0000422 cp = line;
423
424 device = parse_word(&cp);
425 mntpnt = parse_word(&cp);
426 type = parse_word(&cp);
427 opts = parse_word(&cp);
428 freq = parse_word(&cp);
429 passno = parse_word(&cp);
430
431 if (!device)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000432 return 0; /* Allow blank lines */
433
Mike Frysinger51a43b42005-09-24 07:11:16 +0000434 if (!mntpnt || !type)
435 return -1;
436
437 parse_escape(device);
438 parse_escape(mntpnt);
439 parse_escape(type);
440 parse_escape(opts);
441 parse_escape(freq);
442 parse_escape(passno);
443
444 dev = blkid_get_devname(cache, device, NULL);
445 if (dev)
446 device = dev;
447
448 if (strchr(type, ','))
449 type = 0;
450
451 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
452 freq ? atoi(freq) : -1,
453 passno ? atoi(passno) : -1);
454 if (dev)
455 free(dev);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000456
Mike Frysinger51a43b42005-09-24 07:11:16 +0000457 if (!fs)
458 return -1;
459 *ret_fs = fs;
460 return 0;
461}
462
463static void interpret_type(struct fs_info *fs)
464{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000465 char *t;
466
Mike Frysinger51a43b42005-09-24 07:11:16 +0000467 if (strcmp(fs->type, "auto") != 0)
468 return;
469 t = blkid_get_tag_value(cache, "TYPE", fs->device);
470 if (t) {
471 free(fs->type);
472 fs->type = t;
473 }
474}
475
476/*
477 * Load the filesystem database from /etc/fstab
478 */
479static void load_fs_info(const char *filename)
480{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000481 FILE *f;
482 char buf[1024];
483 int lineno = 0;
484 int old_fstab = 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000485 struct fs_info *fs;
486
487 if ((f = fopen(filename, "r")) == NULL) {
"Vladimir N. Oleynik"368f6642005-10-12 16:45:21 +0000488 bb_perror_msg("WARNING: couldn't open %s", filename);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000489 return;
490 }
491 while (!feof(f)) {
492 lineno++;
493 if (!fgets(buf, sizeof(buf), f))
494 break;
495 buf[sizeof(buf)-1] = 0;
496 if (parse_fstab_line(buf, &fs) < 0) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000497 bb_error_msg("WARNING: bad format "
498 "on line %d of %s\n", lineno, filename);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000499 continue;
500 }
501 if (!fs)
502 continue;
503 if (fs->passno < 0)
504 fs->passno = 0;
505 else
506 old_fstab = 0;
507 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000508
Mike Frysinger51a43b42005-09-24 07:11:16 +0000509 fclose(f);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000510
Mike Frysinger51a43b42005-09-24 07:11:16 +0000511 if (old_fstab) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000512 fputs("\007\007\007"
Mike Frysinger51a43b42005-09-24 07:11:16 +0000513 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000514 " field. I will kludge around things for you, but you\n"
515 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
516
Mike Frysinger51a43b42005-09-24 07:11:16 +0000517 for (fs = filesys_info; fs; fs = fs->next) {
518 fs->passno = 1;
519 }
520 }
521}
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000522
Mike Frysinger51a43b42005-09-24 07:11:16 +0000523/* Lookup filesys in /etc/fstab and return the corresponding entry. */
524static struct fs_info *lookup(char *filesys)
525{
526 struct fs_info *fs;
527
528 /* No filesys name given. */
529 if (filesys == NULL)
530 return NULL;
531
532 for (fs = filesys_info; fs; fs = fs->next) {
533 if (!strcmp(filesys, fs->device) ||
534 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
535 break;
536 }
537
538 return fs;
539}
540
541/* Find fsck program for a given fs type. */
542static char *find_fsck(char *type)
543{
544 char *s;
545 const char *tpl;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000546 char *p = string_copy(fsck_path);
547 struct stat st;
548
549 /* Are we looking for a program or just a type? */
550 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
551
552 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000553 s = bb_xasprintf(tpl, s, type);
554 if (stat(s, &st) == 0) break;
555 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000556 }
557 free(p);
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000558 return(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000559}
560
561static int progress_active(void)
562{
563 struct fsck_instance *inst;
564
565 for (inst = instance_list; inst; inst = inst->next) {
566 if (inst->flags & FLAG_DONE)
567 continue;
568 if (inst->flags & FLAG_PROGRESS)
569 return 1;
570 }
571 return 0;
572}
573
574/*
575 * Execute a particular fsck program, and link it into the list of
576 * child processes we are waiting for.
577 */
578static int execute(const char *type, const char *device, const char *mntpt,
579 int interactive)
580{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000581 char *s, *argv[80];
582 char *prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000583 int argc, i;
584 struct fsck_instance *inst, *p;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000585 pid_t pid;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000586
587 inst = malloc(sizeof(struct fsck_instance));
588 if (!inst)
589 return ENOMEM;
590 memset(inst, 0, sizeof(struct fsck_instance));
591
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000592 prog = bb_xasprintf("fsck.%s", type);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000593 argv[0] = prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000594 argc = 1;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000595
Mike Frysinger51a43b42005-09-24 07:11:16 +0000596 for (i=0; i <num_args; i++)
597 argv[argc++] = string_copy(args[i]);
598
599 if (progress && !progress_active()) {
600 if ((strcmp(type, "ext2") == 0) ||
601 (strcmp(type, "ext3") == 0)) {
602 char tmp[80];
603 snprintf(tmp, 80, "-C%d", progress_fd);
604 argv[argc++] = string_copy(tmp);
605 inst->flags |= FLAG_PROGRESS;
606 }
607 }
608
609 argv[argc++] = string_copy(device);
610 argv[argc] = 0;
611
612 s = find_fsck(prog);
613 if (s == NULL) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000614 bb_error_msg("%s: not found", prog);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000615 return ENOENT;
616 }
617
618 if (verbose || noexecute) {
619 printf("[%s (%d) -- %s] ", s, num_running,
620 mntpt ? mntpt : device);
621 for (i=0; i < argc; i++)
622 printf("%s ", argv[i]);
623 printf("\n");
624 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000625
Mike Frysinger51a43b42005-09-24 07:11:16 +0000626 /* Fork and execute the correct program. */
627 if (noexecute)
628 pid = -1;
629 else if ((pid = fork()) < 0) {
630 perror("fork");
631 return errno;
632 } else if (pid == 0) {
633 if (!interactive)
634 close(0);
635 (void) execv(s, argv);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000636 bb_perror_msg_and_die("%s", argv[0]);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000637 }
638
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000639 for (i = 1; i < argc; i++)
Mike Frysinger51a43b42005-09-24 07:11:16 +0000640 free(argv[i]);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000641
642 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000643 inst->pid = pid;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000644 inst->prog = prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000645 inst->type = string_copy(type);
646 inst->device = string_copy(device);
647 inst->base_device = base_device(device);
648 inst->start_time = time(0);
649 inst->next = NULL;
650
651 /*
652 * Find the end of the list, so we add the instance on at the end.
653 */
654 for (p = instance_list; p && p->next; p = p->next);
655
656 if (p)
657 p->next = inst;
658 else
659 instance_list = inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000660
Mike Frysinger51a43b42005-09-24 07:11:16 +0000661 return 0;
662}
663
664/*
665 * Send a signal to all outstanding fsck child processes
666 */
667static int kill_all(int signum)
668{
669 struct fsck_instance *inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000670 int n = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000671
672 for (inst = instance_list; inst; inst = inst->next) {
673 if (inst->flags & FLAG_DONE)
674 continue;
675 kill(inst->pid, signum);
676 n++;
677 }
678 return n;
679}
680
681/*
682 * Wait for one child process to exit; when it does, unlink it from
683 * the list of executing child processes, and return it.
684 */
685static struct fsck_instance *wait_one(int flags)
686{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000687 int status;
688 int sig;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000689 struct fsck_instance *inst, *inst2, *prev;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000690 pid_t pid;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000691
692 if (!instance_list)
693 return NULL;
694
695 if (noexecute) {
696 inst = instance_list;
697 prev = 0;
698#ifdef RANDOM_DEBUG
699 while (inst->next && (random() & 1)) {
700 prev = inst;
701 inst = inst->next;
702 }
703#endif
704 inst->exit_status = 0;
705 goto ret_inst;
706 }
707
708 /*
709 * gcc -Wall fails saving throw against stupidity
710 * (inst and prev are thought to be uninitialized variables)
711 */
712 inst = prev = NULL;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000713
Mike Frysinger51a43b42005-09-24 07:11:16 +0000714 do {
715 pid = waitpid(-1, &status, flags);
716 if (cancel_requested && !kill_sent) {
717 kill_all(SIGTERM);
718 kill_sent++;
719 }
720 if ((pid == 0) && (flags & WNOHANG))
721 return NULL;
722 if (pid < 0) {
723 if ((errno == EINTR) || (errno == EAGAIN))
724 continue;
725 if (errno == ECHILD) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000726 bb_error_msg("wait: No more child process?!?");
Mike Frysinger51a43b42005-09-24 07:11:16 +0000727 return NULL;
728 }
729 perror("wait");
730 continue;
731 }
732 for (prev = 0, inst = instance_list;
733 inst;
734 prev = inst, inst = inst->next) {
735 if (inst->pid == pid)
736 break;
737 }
738 } while (!inst);
739
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000740 if (WIFEXITED(status))
Mike Frysinger51a43b42005-09-24 07:11:16 +0000741 status = WEXITSTATUS(status);
742 else if (WIFSIGNALED(status)) {
743 sig = WTERMSIG(status);
744 if (sig == SIGINT) {
745 status = EXIT_UNCORRECTED;
746 } else {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000747 printf("Warning... %s for device %s exited "
748 "with signal %d.\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000749 inst->prog, inst->device, sig);
750 status = EXIT_ERROR;
751 }
752 } else {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000753 printf("%s %s: status is %x, should never happen.\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000754 inst->prog, inst->device, status);
755 status = EXIT_ERROR;
756 }
757 inst->exit_status = status;
758 if (progress && (inst->flags & FLAG_PROGRESS) &&
759 !progress_active()) {
760 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
761 if (inst2->flags & FLAG_DONE)
762 continue;
763 if (strcmp(inst2->type, "ext2") &&
764 strcmp(inst2->type, "ext3"))
765 continue;
766 /*
767 * If we've just started the fsck, wait a tiny
768 * bit before sending the kill, to give it
769 * time to set up the signal handler
770 */
771 if (inst2->start_time < time(0)+2) {
772 if (fork() == 0) {
773 sleep(1);
774 kill(inst2->pid, SIGUSR1);
775 exit(0);
776 }
777 } else
778 kill(inst2->pid, SIGUSR1);
779 inst2->flags |= FLAG_PROGRESS;
780 break;
781 }
782 }
783ret_inst:
784 if (prev)
785 prev->next = inst->next;
786 else
787 instance_list = inst->next;
788 if (verbose > 1)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000789 printf("Finished with %s (exit status %d)\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000790 inst->device, inst->exit_status);
791 num_running--;
792 return inst;
793}
794
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000795#define FLAG_WAIT_ALL 0
796#define FLAG_WAIT_ATLEAST_ONE 1
Mike Frysinger51a43b42005-09-24 07:11:16 +0000797/*
798 * Wait until all executing child processes have exited; return the
799 * logical OR of all of their exit code values.
800 */
801static int wait_many(int flags)
802{
803 struct fsck_instance *inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000804 int global_status = 0;
805 int wait_flags = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000806
807 while ((inst = wait_one(wait_flags))) {
808 global_status |= inst->exit_status;
809 free_instance(inst);
810#ifdef RANDOM_DEBUG
811 if (noexecute && (flags & WNOHANG) && !(random() % 3))
812 break;
813#endif
814 if (flags & FLAG_WAIT_ATLEAST_ONE)
815 wait_flags = WNOHANG;
816 }
817 return global_status;
818}
819
820/*
821 * Run the fsck program on a particular device
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000822 *
Mike Frysinger51a43b42005-09-24 07:11:16 +0000823 * If the type is specified using -t, and it isn't prefixed with "no"
824 * (as in "noext2") and only one filesystem type is specified, then
825 * use that type regardless of what is specified in /etc/fstab.
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000826 *
Mike Frysinger51a43b42005-09-24 07:11:16 +0000827 * If the type isn't specified by the user, then use either the type
828 * specified in /etc/fstab, or DEFAULT_FSTYPE.
829 */
830static void fsck_device(struct fs_info *fs, int interactive)
831{
832 const char *type;
833 int retval;
834
835 interpret_type(fs);
836
837 if (strcmp(fs->type, "auto") != 0)
838 type = fs->type;
839 else if (fstype && strncmp(fstype, "no", 2) &&
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000840 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
Mike Frysinger51a43b42005-09-24 07:11:16 +0000841 !strchr(fstype, ','))
842 type = fstype;
843 else
844 type = DEFAULT_FSTYPE;
845
846 num_running++;
847 retval = execute(type, fs->device, fs->mountpt, interactive);
848 if (retval) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000849 bb_error_msg("Error %d while executing fsck.%s for %s",
850 retval, type, fs->device);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000851 num_running--;
852 }
853}
854
855
856/*
857 * Deal with the fsck -t argument.
858 */
859struct fs_type_compile {
860 char **list;
861 int *type;
862 int negate;
863} fs_type_compiled;
864
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000865#define FS_TYPE_NORMAL 0
866#define FS_TYPE_OPT 1
867#define FS_TYPE_NEGOPT 2
Mike Frysinger51a43b42005-09-24 07:11:16 +0000868
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000869static const char fs_type_syntax_error[] =
870"Either all or none of the filesystem types passed to -t must be prefixed\n"
871 "with 'no' or '!'.";
Mike Frysinger51a43b42005-09-24 07:11:16 +0000872
873static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
874{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000875 char *cp, *list, *s;
876 int num = 2;
877 int negate, first_negate = 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000878
879 if (fs_type) {
880 for (cp=fs_type; *cp; cp++) {
881 if (*cp == ',')
882 num++;
883 }
884 }
885
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000886 cmp->list = xcalloc(num, sizeof(char *));
887 cmp->type = xcalloc(num, sizeof(int));
Mike Frysinger51a43b42005-09-24 07:11:16 +0000888 cmp->negate = 0;
889
890 if (!fs_type)
891 return;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000892
Mike Frysinger51a43b42005-09-24 07:11:16 +0000893 list = string_copy(fs_type);
894 num = 0;
895 s = strtok(list, ",");
896 while(s) {
897 negate = 0;
898 if (strncmp(s, "no", 2) == 0) {
899 s += 2;
900 negate = 1;
901 } else if (*s == '!') {
902 s++;
903 negate = 1;
904 }
905 if (strcmp(s, "loop") == 0)
906 /* loop is really short-hand for opts=loop */
907 goto loop_special_case;
908 else if (strncmp(s, "opts=", 5) == 0) {
909 s += 5;
910 loop_special_case:
911 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
912 } else {
913 if (first_negate) {
914 cmp->negate = negate;
915 first_negate = 0;
916 }
917 if ((negate && !cmp->negate) ||
918 (!negate && cmp->negate)) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000919 bb_error_msg_and_die("%s", fs_type_syntax_error);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000920 }
921 }
922#if 0
923 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
924#endif
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000925 cmp->list[num++] = string_copy(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000926 s = strtok(NULL, ",");
927 }
928 free(list);
929}
930
931/*
932 * This function returns true if a particular option appears in a
933 * comma-delimited options list
934 */
935static int opt_in_list(char *opt, char *optlist)
936{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000937 char *list, *s;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000938
939 if (!optlist)
940 return 0;
941 list = string_copy(optlist);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000942
Mike Frysinger51a43b42005-09-24 07:11:16 +0000943 s = strtok(list, ",");
944 while(s) {
945 if (strcmp(s, opt) == 0) {
946 free(list);
947 return 1;
948 }
949 s = strtok(NULL, ",");
950 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000951 free(list);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000952 return 0;
953}
954
955/* See if the filesystem matches the criteria given by the -t option */
956static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
957{
958 int n, ret = 0, checked_type = 0;
959 char *cp;
960
961 if (cmp->list == 0 || cmp->list[0] == 0)
962 return 1;
963
964 for (n=0; (cp = cmp->list[n]); n++) {
965 switch (cmp->type[n]) {
966 case FS_TYPE_NORMAL:
967 checked_type++;
968 if (strcmp(cp, fs->type) == 0) {
969 ret = 1;
970 }
971 break;
972 case FS_TYPE_NEGOPT:
973 if (opt_in_list(cp, fs->opts))
974 return 0;
975 break;
976 case FS_TYPE_OPT:
977 if (!opt_in_list(cp, fs->opts))
978 return 0;
979 break;
980 }
981 }
982 if (checked_type == 0)
983 return 1;
984 return (cmp->negate ? !ret : ret);
985}
986
987/* Check if we should ignore this filesystem. */
988static int ignore(struct fs_info *fs)
989{
"Vladimir N. Oleynik"61ff4b32005-11-26 10:33:55 +0000990 int wanted;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000991 char *s;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000992
993 /*
994 * If the pass number is 0, ignore it.
995 */
996 if (fs->passno == 0)
997 return 1;
998
999 interpret_type(fs);
1000
1001 /*
1002 * If a specific fstype is specified, and it doesn't match,
1003 * ignore it.
1004 */
1005 if (!fs_match(fs, &fs_type_compiled)) return 1;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001006
Mike Frysinger51a43b42005-09-24 07:11:16 +00001007 /* Are we ignoring this type? */
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +00001008 if(compare_string_array(ignored_types, fs->type) >= 0)
"Vladimir N. Oleynik"61ff4b32005-11-26 10:33:55 +00001009 return 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001010
1011 /* Do we really really want to check this fs? */
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +00001012 wanted = compare_string_array(really_wanted, fs->type) >= 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001013
1014 /* See if the <fsck.fs> program is available. */
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001015 s = find_fsck(fs->type);
1016 if (s == NULL) {
Mike Frysinger51a43b42005-09-24 07:11:16 +00001017 if (wanted)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001018 bb_error_msg("cannot check %s: fsck.%s not found",
Mike Frysinger51a43b42005-09-24 07:11:16 +00001019 fs->device, fs->type);
1020 return 1;
1021 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001022 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001023
1024 /* We can and want to check this file system type. */
1025 return 0;
1026}
1027
1028/*
1029 * Returns TRUE if a partition on the same disk is already being
1030 * checked.
1031 */
1032static int device_already_active(char *device)
1033{
1034 struct fsck_instance *inst;
1035 char *base;
1036
1037 if (force_all_parallel)
1038 return 0;
1039
1040#ifdef BASE_MD
1041 /* Don't check a soft raid disk with any other disk */
1042 if (instance_list &&
1043 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1044 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1045 return 1;
1046#endif
1047
1048 base = base_device(device);
1049 /*
1050 * If we don't know the base device, assume that the device is
1051 * already active if there are any fsck instances running.
1052 */
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001053 if (!base)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001054 return (instance_list != 0);
1055 for (inst = instance_list; inst; inst = inst->next) {
1056 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1057 free(base);
1058 return 1;
1059 }
1060 }
1061 free(base);
1062 return 0;
1063}
1064
1065/* Check all file systems, using the /etc/fstab table. */
1066static int check_all(void)
1067{
1068 struct fs_info *fs = NULL;
1069 int status = EXIT_OK;
1070 int not_done_yet = 1;
1071 int passno = 1;
1072 int pass_done;
1073
1074 if (verbose)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001075 fputs("Checking all file systems.\n", stdout);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001076
1077 /*
1078 * Do an initial scan over the filesystem; mark filesystems
1079 * which should be ignored as done, and resolve any "auto"
1080 * filesystem types (done as a side-effect of calling ignore()).
1081 */
1082 for (fs = filesys_info; fs; fs = fs->next) {
1083 if (ignore(fs))
1084 fs->flags |= FLAG_DONE;
1085 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001086
Mike Frysinger51a43b42005-09-24 07:11:16 +00001087 /*
1088 * Find and check the root filesystem.
1089 */
1090 if (!parallel_root) {
1091 for (fs = filesys_info; fs; fs = fs->next) {
1092 if (!strcmp(fs->mountpt, "/"))
1093 break;
1094 }
1095 if (fs) {
1096 if (!skip_root && !ignore(fs)) {
1097 fsck_device(fs, 1);
1098 status |= wait_many(FLAG_WAIT_ALL);
1099 if (status > EXIT_NONDESTRUCT)
1100 return status;
1101 }
1102 fs->flags |= FLAG_DONE;
1103 }
1104 }
1105 /*
1106 * This is for the bone-headed user who enters the root
1107 * filesystem twice. Skip root will skep all root entries.
1108 */
1109 if (skip_root)
1110 for (fs = filesys_info; fs; fs = fs->next)
1111 if (!strcmp(fs->mountpt, "/"))
1112 fs->flags |= FLAG_DONE;
1113
1114 while (not_done_yet) {
1115 not_done_yet = 0;
1116 pass_done = 1;
1117
1118 for (fs = filesys_info; fs; fs = fs->next) {
1119 if (cancel_requested)
1120 break;
1121 if (fs->flags & FLAG_DONE)
1122 continue;
1123 /*
1124 * If the filesystem's pass number is higher
1125 * than the current pass number, then we don't
1126 * do it yet.
1127 */
1128 if (fs->passno > passno) {
1129 not_done_yet++;
1130 continue;
1131 }
1132 /*
1133 * If a filesystem on a particular device has
1134 * already been spawned, then we need to defer
1135 * this to another pass.
1136 */
1137 if (device_already_active(fs->device)) {
1138 pass_done = 0;
1139 continue;
1140 }
1141 /*
1142 * Spawn off the fsck process
1143 */
1144 fsck_device(fs, serialize);
1145 fs->flags |= FLAG_DONE;
1146
1147 /*
1148 * Only do one filesystem at a time, or if we
1149 * have a limit on the number of fsck's extant
1150 * at one time, apply that limit.
1151 */
1152 if (serialize ||
1153 (max_running && (num_running >= max_running))) {
1154 pass_done = 0;
1155 break;
1156 }
1157 }
1158 if (cancel_requested)
1159 break;
1160 if (verbose > 1)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001161 printf("--waiting-- (pass %d)\n", passno);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001162 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1163 FLAG_WAIT_ATLEAST_ONE);
1164 if (pass_done) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001165 if (verbose > 1)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001166 printf("----------------------------------\n");
1167 passno++;
1168 } else
1169 not_done_yet++;
1170 }
1171 if (cancel_requested && !kill_sent) {
1172 kill_all(SIGTERM);
1173 kill_sent++;
1174 }
1175 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1176 return status;
1177}
1178
1179#if 0
1180static void usage(void)
1181{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001182 fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001183 exit(EXIT_USAGE);
1184}
1185#endif
1186
Mike Frysinger51a43b42005-09-24 07:11:16 +00001187static void signal_cancel(int sig FSCK_ATTR((unused)))
1188{
1189 cancel_requested++;
1190}
Mike Frysinger51a43b42005-09-24 07:11:16 +00001191
1192static void PRS(int argc, char *argv[])
1193{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001194 int i, j;
1195 char *arg, *dev, *tmp = 0;
1196 char options[128];
1197 int opt = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001198 int opts_for_fsck = 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001199 struct sigaction sa;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001200
1201 /*
1202 * Set up signal action
1203 */
1204 memset(&sa, 0, sizeof(struct sigaction));
1205 sa.sa_handler = signal_cancel;
1206 sigaction(SIGINT, &sa, 0);
1207 sigaction(SIGTERM, &sa, 0);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001208
Mike Frysinger51a43b42005-09-24 07:11:16 +00001209 num_devices = 0;
1210 num_args = 0;
1211 instance_list = 0;
1212
Mike Frysinger51a43b42005-09-24 07:11:16 +00001213 for (i=1; i < argc; i++) {
1214 arg = argv[i];
1215 if (!arg)
1216 continue;
1217 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1218 if (num_devices >= MAX_DEVICES) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001219 bb_error_msg_and_die("too many devices");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001220 }
1221 dev = blkid_get_devname(cache, arg, NULL);
1222 if (!dev && strchr(arg, '=')) {
1223 /*
1224 * Check to see if we failed because
1225 * /proc/partitions isn't found.
1226 */
1227 if (access("/proc/partitions", R_OK) < 0) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001228 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1229 "Is /proc mounted?");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001230 }
1231 /*
1232 * Check to see if this is because
1233 * we're not running as root
1234 */
1235 if (geteuid())
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001236 bb_error_msg_and_die(
Mike Frysinger51a43b42005-09-24 07:11:16 +00001237 "Must be root to scan for matching filesystems: %s\n", arg);
1238 else
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001239 bb_error_msg_and_die(
1240 "Couldn't find matching filesystem: %s", arg);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001241 }
1242 devices[num_devices++] = dev ? dev : string_copy(arg);
1243 continue;
1244 }
1245 if (arg[0] != '-' || opts_for_fsck) {
1246 if (num_args >= MAX_ARGS) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001247 bb_error_msg_and_die("too many arguments");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001248 }
1249 args[num_args++] = string_copy(arg);
1250 continue;
1251 }
1252 for (j=1; arg[j]; j++) {
1253 if (opts_for_fsck) {
1254 options[++opt] = arg[j];
1255 continue;
1256 }
1257 switch (arg[j]) {
1258 case 'A':
1259 doall++;
1260 break;
1261 case 'C':
1262 progress++;
1263 if (arg[j+1]) {
1264 progress_fd = string_to_int(arg+j+1);
1265 if (progress_fd < 0)
1266 progress_fd = 0;
1267 else
1268 goto next_arg;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001269 } else if ((i+1) < argc &&
Mike Frysinger51a43b42005-09-24 07:11:16 +00001270 !strncmp(argv[i+1], "-", 1) == 0) {
1271 progress_fd = string_to_int(argv[i]);
1272 if (progress_fd < 0)
1273 progress_fd = 0;
1274 else {
1275 goto next_arg;
1276 i++;
1277 }
1278 }
1279 break;
1280 case 'V':
1281 verbose++;
1282 break;
1283 case 'N':
1284 noexecute++;
1285 break;
1286 case 'R':
1287 skip_root++;
1288 break;
1289 case 'T':
1290 notitle++;
1291 break;
1292 case 'M':
1293 like_mount++;
1294 break;
1295 case 'P':
1296 parallel_root++;
1297 break;
1298 case 's':
1299 serialize++;
1300 break;
1301 case 't':
1302 tmp = 0;
1303 if (fstype)
1304 usage();
1305 if (arg[j+1])
1306 tmp = arg+j+1;
1307 else if ((i+1) < argc)
1308 tmp = argv[++i];
1309 else
1310 usage();
1311 fstype = string_copy(tmp);
1312 compile_fs_type(fstype, &fs_type_compiled);
1313 goto next_arg;
1314 case '-':
1315 opts_for_fsck++;
1316 break;
1317 case '?':
1318 usage();
1319 break;
1320 default:
1321 options[++opt] = arg[j];
1322 break;
1323 }
1324 }
1325 next_arg:
1326 if (opt) {
1327 options[0] = '-';
1328 options[++opt] = '\0';
1329 if (num_args >= MAX_ARGS) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001330 bb_error_msg("too many arguments");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001331 }
1332 args[num_args++] = string_copy(options);
1333 opt = 0;
1334 }
1335 }
1336 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1337 force_all_parallel++;
1338 if ((tmp = getenv("FSCK_MAX_INST")))
1339 max_running = atoi(tmp);
1340}
1341
1342int fsck_main(int argc, char *argv[])
1343{
1344 int i, status = 0;
1345 int interactive = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001346 const char *fstab;
1347 struct fs_info *fs;
1348
1349 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1350 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1351
Mike Frysinger51a43b42005-09-24 07:11:16 +00001352 blkid_get_cache(&cache, NULL);
1353 PRS(argc, argv);
1354
1355 if (!notitle)
1356 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1357
1358 fstab = getenv("FSTAB_FILE");
1359 if (!fstab)
1360 fstab = _PATH_MNTTAB;
1361 load_fs_info(fstab);
1362
"Vladimir N. Oleynik"350865e2005-11-26 11:01:23 +00001363 fsck_path = e2fs_set_sbin_path();
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001364
Mike Frysinger51a43b42005-09-24 07:11:16 +00001365 if ((num_devices == 1) || (serialize))
1366 interactive = 1;
1367
1368 /* If -A was specified ("check all"), do that! */
1369 if (doall)
1370 return check_all();
1371
1372 if (num_devices == 0) {
1373 serialize++;
1374 interactive++;
1375 return check_all();
1376 }
1377 for (i = 0 ; i < num_devices; i++) {
1378 if (cancel_requested) {
1379 if (!kill_sent) {
1380 kill_all(SIGTERM);
1381 kill_sent++;
1382 }
1383 break;
1384 }
1385 fs = lookup(devices[i]);
1386 if (!fs) {
1387 fs = create_fs_device(devices[i], 0, "auto",
1388 0, -1, -1);
1389 if (!fs)
1390 continue;
1391 }
1392 fsck_device(fs, interactive);
1393 if (serialize ||
1394 (max_running && (num_running >= max_running))) {
1395 struct fsck_instance *inst;
1396
1397 inst = wait_one(0);
1398 if (inst) {
1399 status |= inst->exit_status;
1400 free_instance(inst);
1401 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001402 if (verbose > 1)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001403 printf("----------------------------------\n");
1404 }
1405 }
1406 status |= wait_many(FLAG_WAIT_ALL);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001407 blkid_put_cache(cache);
1408 return status;
1409}