blob: 7799faec0b8d31a4d8a95ce72cbfd92d325ed2df [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>
30#include <sys/signal.h>
31#include <sys/stat.h>
32#include <limits.h>
33#include <stdio.h>
34#include <ctype.h>
35#include <string.h>
36#include <time.h>
37#include <stdlib.h>
38#include <errno.h>
39#include <paths.h>
40#include <unistd.h>
41#include <errno.h>
42#include <malloc.h>
43#include <signal.h>
44
45#include "fsck.h"
46#include "blkid/blkid.h"
47
48#include "e2fsbb.h"
49
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +000050#include "busybox.h"
51
Mike Frysinger51a43b42005-09-24 07:11:16 +000052#ifndef _PATH_MNTTAB
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000053#define _PATH_MNTTAB "/etc/fstab"
Mike Frysinger51a43b42005-09-24 07:11:16 +000054#endif
55
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +000056/*
"Vladimir N. Oleynik"3ebb8952005-10-12 12:24:01 +000057 * fsck.h
58 */
59
60#ifndef DEFAULT_FSTYPE
61#define DEFAULT_FSTYPE "ext2"
62#endif
63
64#define MAX_DEVICES 32
65#define MAX_ARGS 32
66
67/*
68 * Internal structure for mount tabel entries.
69 */
70
71struct fs_info {
72 char *device;
73 char *mountpt;
74 char *type;
75 char *opts;
76 int freq;
77 int passno;
78 int flags;
79 struct fs_info *next;
80};
81
82#define FLAG_DONE 1
83#define FLAG_PROGRESS 2
84
85/*
86 * Structure to allow exit codes to be stored
87 */
88struct fsck_instance {
89 int pid;
90 int flags;
91 int exit_status;
92 time_t start_time;
93 char * prog;
94 char * type;
95 char * device;
96 char * base_device;
97 struct fsck_instance *next;
98};
99
100/*
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +0000101 * base_device.c
102 *
103 * Return the "base device" given a particular device; this is used to
104 * assure that we only fsck one partition on a particular drive at any
105 * one time. Otherwise, the disk heads will be seeking all over the
106 * place. If the base device can not be determined, return NULL.
107 *
108 * The base_device() function returns an allocated string which must
109 * be freed.
110 *
111 */
112
113
114#ifdef CONFIG_FEATURE_DEVFS
115/*
116 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
117 * pathames.
118 */
119static const char *devfs_hier[] = {
120 "host", "bus", "target", "lun", 0
121};
122#endif
123
124static char *base_device(const char *device)
125{
126 char *str, *cp;
127#ifdef CONFIG_FEATURE_DEVFS
128 const char **hier, *disk;
129 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;
285static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000286static char *fsck_path;
287static blkid_cache cache;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000288
289static char *string_copy(const char *s)
290{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000291 char *ret;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000292
293 if (!s)
294 return 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000295 ret = strdup(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000296 return ret;
297}
298
299static int string_to_int(const char *s)
300{
301 long l;
302 char *p;
303
304 l = strtol(s, &p, 0);
305 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
306 return -1;
307 else
308 return (int) l;
309}
310
Mike Frysinger51a43b42005-09-24 07:11:16 +0000311static char *skip_over_blank(char *cp)
312{
313 while (*cp && isspace(*cp))
314 cp++;
315 return cp;
316}
317
318static char *skip_over_word(char *cp)
319{
320 while (*cp && !isspace(*cp))
321 cp++;
322 return cp;
323}
324
325static void strip_line(char *line)
326{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000327 char *p;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000328
329 while (*line) {
330 p = line + strlen(line) - 1;
331 if ((*p == '\n') || (*p == '\r'))
332 *p = 0;
333 else
334 break;
335 }
336}
337
338static char *parse_word(char **buf)
339{
340 char *word, *next;
341
342 word = *buf;
343 if (*word == 0)
344 return 0;
345
346 word = skip_over_blank(word);
347 next = skip_over_word(word);
348 if (*next)
349 *next++ = 0;
350 *buf = next;
351 return word;
352}
353
354static void parse_escape(char *word)
355{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000356 char *q, c;
357 const char *p;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000358
359 if (!word)
360 return;
361
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000362 for (p = q = word; *p; q++) {
363 c = *p++;
364 if (c != '\\') {
365 *q = c;
366 } else {
367 *q = bb_process_escape_sequence(&p);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000368 }
Mike Frysinger51a43b42005-09-24 07:11:16 +0000369 }
370 *q = 0;
371}
372
373static void free_instance(struct fsck_instance *i)
374{
375 if (i->prog)
376 free(i->prog);
377 if (i->device)
378 free(i->device);
379 if (i->base_device)
380 free(i->base_device);
381 free(i);
382 return;
383}
384
385static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000386 const char *type, const char *opts,
Mike Frysinger51a43b42005-09-24 07:11:16 +0000387 int freq, int passno)
388{
389 struct fs_info *fs;
390
391 if (!(fs = malloc(sizeof(struct fs_info))))
392 return NULL;
393
394 fs->device = string_copy(device);
395 fs->mountpt = string_copy(mntpnt);
396 fs->type = string_copy(type);
397 fs->opts = string_copy(opts ? opts : "");
398 fs->freq = freq;
399 fs->passno = passno;
400 fs->flags = 0;
401 fs->next = NULL;
402
403 if (!filesys_info)
404 filesys_info = fs;
405 else
406 filesys_last->next = fs;
407 filesys_last = fs;
408
409 return fs;
410}
411
412
413
414static int parse_fstab_line(char *line, struct fs_info **ret_fs)
415{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000416 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000417 struct fs_info *fs;
418
419 *ret_fs = 0;
420 strip_line(line);
421 if ((cp = strchr(line, '#')))
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000422 *cp = 0; /* Ignore everything after the comment char */
Mike Frysinger51a43b42005-09-24 07:11:16 +0000423 cp = line;
424
425 device = parse_word(&cp);
426 mntpnt = parse_word(&cp);
427 type = parse_word(&cp);
428 opts = parse_word(&cp);
429 freq = parse_word(&cp);
430 passno = parse_word(&cp);
431
432 if (!device)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000433 return 0; /* Allow blank lines */
434
Mike Frysinger51a43b42005-09-24 07:11:16 +0000435 if (!mntpnt || !type)
436 return -1;
437
438 parse_escape(device);
439 parse_escape(mntpnt);
440 parse_escape(type);
441 parse_escape(opts);
442 parse_escape(freq);
443 parse_escape(passno);
444
445 dev = blkid_get_devname(cache, device, NULL);
446 if (dev)
447 device = dev;
448
449 if (strchr(type, ','))
450 type = 0;
451
452 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
453 freq ? atoi(freq) : -1,
454 passno ? atoi(passno) : -1);
455 if (dev)
456 free(dev);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000457
Mike Frysinger51a43b42005-09-24 07:11:16 +0000458 if (!fs)
459 return -1;
460 *ret_fs = fs;
461 return 0;
462}
463
464static void interpret_type(struct fs_info *fs)
465{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000466 char *t;
467
Mike Frysinger51a43b42005-09-24 07:11:16 +0000468 if (strcmp(fs->type, "auto") != 0)
469 return;
470 t = blkid_get_tag_value(cache, "TYPE", fs->device);
471 if (t) {
472 free(fs->type);
473 fs->type = t;
474 }
475}
476
477/*
478 * Load the filesystem database from /etc/fstab
479 */
480static void load_fs_info(const char *filename)
481{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000482 FILE *f;
483 char buf[1024];
484 int lineno = 0;
485 int old_fstab = 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000486 struct fs_info *fs;
487
488 if ((f = fopen(filename, "r")) == NULL) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000489 bb_perror_msg("WARNING: couldn't open %s: %m", filename);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000490 return;
491 }
492 while (!feof(f)) {
493 lineno++;
494 if (!fgets(buf, sizeof(buf), f))
495 break;
496 buf[sizeof(buf)-1] = 0;
497 if (parse_fstab_line(buf, &fs) < 0) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000498 bb_error_msg("WARNING: bad format "
499 "on line %d of %s\n", lineno, filename);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000500 continue;
501 }
502 if (!fs)
503 continue;
504 if (fs->passno < 0)
505 fs->passno = 0;
506 else
507 old_fstab = 0;
508 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000509
Mike Frysinger51a43b42005-09-24 07:11:16 +0000510 fclose(f);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000511
Mike Frysinger51a43b42005-09-24 07:11:16 +0000512 if (old_fstab) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000513 fputs("\007\007\007"
Mike Frysinger51a43b42005-09-24 07:11:16 +0000514 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000515 " field. I will kludge around things for you, but you\n"
516 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
517
Mike Frysinger51a43b42005-09-24 07:11:16 +0000518 for (fs = filesys_info; fs; fs = fs->next) {
519 fs->passno = 1;
520 }
521 }
522}
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000523
Mike Frysinger51a43b42005-09-24 07:11:16 +0000524/* Lookup filesys in /etc/fstab and return the corresponding entry. */
525static struct fs_info *lookup(char *filesys)
526{
527 struct fs_info *fs;
528
529 /* No filesys name given. */
530 if (filesys == NULL)
531 return NULL;
532
533 for (fs = filesys_info; fs; fs = fs->next) {
534 if (!strcmp(filesys, fs->device) ||
535 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
536 break;
537 }
538
539 return fs;
540}
541
542/* Find fsck program for a given fs type. */
543static char *find_fsck(char *type)
544{
545 char *s;
546 const char *tpl;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000547 char *p = string_copy(fsck_path);
548 struct stat st;
549
550 /* Are we looking for a program or just a type? */
551 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
552
553 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000554 s = bb_xasprintf(tpl, s, type);
555 if (stat(s, &st) == 0) break;
556 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000557 }
558 free(p);
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000559 return(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000560}
561
562static int progress_active(void)
563{
564 struct fsck_instance *inst;
565
566 for (inst = instance_list; inst; inst = inst->next) {
567 if (inst->flags & FLAG_DONE)
568 continue;
569 if (inst->flags & FLAG_PROGRESS)
570 return 1;
571 }
572 return 0;
573}
574
575/*
576 * Execute a particular fsck program, and link it into the list of
577 * child processes we are waiting for.
578 */
579static int execute(const char *type, const char *device, const char *mntpt,
580 int interactive)
581{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000582 char *s, *argv[80];
583 char *prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000584 int argc, i;
585 struct fsck_instance *inst, *p;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000586 pid_t pid;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000587
588 inst = malloc(sizeof(struct fsck_instance));
589 if (!inst)
590 return ENOMEM;
591 memset(inst, 0, sizeof(struct fsck_instance));
592
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000593 prog = bb_xasprintf("fsck.%s", type);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000594 argv[0] = prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000595 argc = 1;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000596
Mike Frysinger51a43b42005-09-24 07:11:16 +0000597 for (i=0; i <num_args; i++)
598 argv[argc++] = string_copy(args[i]);
599
600 if (progress && !progress_active()) {
601 if ((strcmp(type, "ext2") == 0) ||
602 (strcmp(type, "ext3") == 0)) {
603 char tmp[80];
604 snprintf(tmp, 80, "-C%d", progress_fd);
605 argv[argc++] = string_copy(tmp);
606 inst->flags |= FLAG_PROGRESS;
607 }
608 }
609
610 argv[argc++] = string_copy(device);
611 argv[argc] = 0;
612
613 s = find_fsck(prog);
614 if (s == NULL) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000615 bb_error_msg("%s: not found", prog);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000616 return ENOENT;
617 }
618
619 if (verbose || noexecute) {
620 printf("[%s (%d) -- %s] ", s, num_running,
621 mntpt ? mntpt : device);
622 for (i=0; i < argc; i++)
623 printf("%s ", argv[i]);
624 printf("\n");
625 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000626
Mike Frysinger51a43b42005-09-24 07:11:16 +0000627 /* Fork and execute the correct program. */
628 if (noexecute)
629 pid = -1;
630 else if ((pid = fork()) < 0) {
631 perror("fork");
632 return errno;
633 } else if (pid == 0) {
634 if (!interactive)
635 close(0);
636 (void) execv(s, argv);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000637 bb_perror_msg_and_die("%s", argv[0]);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000638 }
639
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000640 for (i = 1; i < argc; i++)
Mike Frysinger51a43b42005-09-24 07:11:16 +0000641 free(argv[i]);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000642
643 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000644 inst->pid = pid;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000645 inst->prog = prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000646 inst->type = string_copy(type);
647 inst->device = string_copy(device);
648 inst->base_device = base_device(device);
649 inst->start_time = time(0);
650 inst->next = NULL;
651
652 /*
653 * Find the end of the list, so we add the instance on at the end.
654 */
655 for (p = instance_list; p && p->next; p = p->next);
656
657 if (p)
658 p->next = inst;
659 else
660 instance_list = inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000661
Mike Frysinger51a43b42005-09-24 07:11:16 +0000662 return 0;
663}
664
665/*
666 * Send a signal to all outstanding fsck child processes
667 */
668static int kill_all(int signum)
669{
670 struct fsck_instance *inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000671 int n = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000672
673 for (inst = instance_list; inst; inst = inst->next) {
674 if (inst->flags & FLAG_DONE)
675 continue;
676 kill(inst->pid, signum);
677 n++;
678 }
679 return n;
680}
681
682/*
683 * Wait for one child process to exit; when it does, unlink it from
684 * the list of executing child processes, and return it.
685 */
686static struct fsck_instance *wait_one(int flags)
687{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000688 int status;
689 int sig;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000690 struct fsck_instance *inst, *inst2, *prev;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000691 pid_t pid;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000692
693 if (!instance_list)
694 return NULL;
695
696 if (noexecute) {
697 inst = instance_list;
698 prev = 0;
699#ifdef RANDOM_DEBUG
700 while (inst->next && (random() & 1)) {
701 prev = inst;
702 inst = inst->next;
703 }
704#endif
705 inst->exit_status = 0;
706 goto ret_inst;
707 }
708
709 /*
710 * gcc -Wall fails saving throw against stupidity
711 * (inst and prev are thought to be uninitialized variables)
712 */
713 inst = prev = NULL;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000714
Mike Frysinger51a43b42005-09-24 07:11:16 +0000715 do {
716 pid = waitpid(-1, &status, flags);
717 if (cancel_requested && !kill_sent) {
718 kill_all(SIGTERM);
719 kill_sent++;
720 }
721 if ((pid == 0) && (flags & WNOHANG))
722 return NULL;
723 if (pid < 0) {
724 if ((errno == EINTR) || (errno == EAGAIN))
725 continue;
726 if (errno == ECHILD) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000727 bb_error_msg("wait: No more child process?!?");
Mike Frysinger51a43b42005-09-24 07:11:16 +0000728 return NULL;
729 }
730 perror("wait");
731 continue;
732 }
733 for (prev = 0, inst = instance_list;
734 inst;
735 prev = inst, inst = inst->next) {
736 if (inst->pid == pid)
737 break;
738 }
739 } while (!inst);
740
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000741 if (WIFEXITED(status))
Mike Frysinger51a43b42005-09-24 07:11:16 +0000742 status = WEXITSTATUS(status);
743 else if (WIFSIGNALED(status)) {
744 sig = WTERMSIG(status);
745 if (sig == SIGINT) {
746 status = EXIT_UNCORRECTED;
747 } else {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000748 printf("Warning... %s for device %s exited "
749 "with signal %d.\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000750 inst->prog, inst->device, sig);
751 status = EXIT_ERROR;
752 }
753 } else {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000754 printf("%s %s: status is %x, should never happen.\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000755 inst->prog, inst->device, status);
756 status = EXIT_ERROR;
757 }
758 inst->exit_status = status;
759 if (progress && (inst->flags & FLAG_PROGRESS) &&
760 !progress_active()) {
761 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
762 if (inst2->flags & FLAG_DONE)
763 continue;
764 if (strcmp(inst2->type, "ext2") &&
765 strcmp(inst2->type, "ext3"))
766 continue;
767 /*
768 * If we've just started the fsck, wait a tiny
769 * bit before sending the kill, to give it
770 * time to set up the signal handler
771 */
772 if (inst2->start_time < time(0)+2) {
773 if (fork() == 0) {
774 sleep(1);
775 kill(inst2->pid, SIGUSR1);
776 exit(0);
777 }
778 } else
779 kill(inst2->pid, SIGUSR1);
780 inst2->flags |= FLAG_PROGRESS;
781 break;
782 }
783 }
784ret_inst:
785 if (prev)
786 prev->next = inst->next;
787 else
788 instance_list = inst->next;
789 if (verbose > 1)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000790 printf("Finished with %s (exit status %d)\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000791 inst->device, inst->exit_status);
792 num_running--;
793 return inst;
794}
795
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000796#define FLAG_WAIT_ALL 0
797#define FLAG_WAIT_ATLEAST_ONE 1
Mike Frysinger51a43b42005-09-24 07:11:16 +0000798/*
799 * Wait until all executing child processes have exited; return the
800 * logical OR of all of their exit code values.
801 */
802static int wait_many(int flags)
803{
804 struct fsck_instance *inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000805 int global_status = 0;
806 int wait_flags = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000807
808 while ((inst = wait_one(wait_flags))) {
809 global_status |= inst->exit_status;
810 free_instance(inst);
811#ifdef RANDOM_DEBUG
812 if (noexecute && (flags & WNOHANG) && !(random() % 3))
813 break;
814#endif
815 if (flags & FLAG_WAIT_ATLEAST_ONE)
816 wait_flags = WNOHANG;
817 }
818 return global_status;
819}
820
821/*
822 * Run the fsck program on a particular device
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000823 *
Mike Frysinger51a43b42005-09-24 07:11:16 +0000824 * If the type is specified using -t, and it isn't prefixed with "no"
825 * (as in "noext2") and only one filesystem type is specified, then
826 * use that type regardless of what is specified in /etc/fstab.
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000827 *
Mike Frysinger51a43b42005-09-24 07:11:16 +0000828 * If the type isn't specified by the user, then use either the type
829 * specified in /etc/fstab, or DEFAULT_FSTYPE.
830 */
831static void fsck_device(struct fs_info *fs, int interactive)
832{
833 const char *type;
834 int retval;
835
836 interpret_type(fs);
837
838 if (strcmp(fs->type, "auto") != 0)
839 type = fs->type;
840 else if (fstype && strncmp(fstype, "no", 2) &&
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000841 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
Mike Frysinger51a43b42005-09-24 07:11:16 +0000842 !strchr(fstype, ','))
843 type = fstype;
844 else
845 type = DEFAULT_FSTYPE;
846
847 num_running++;
848 retval = execute(type, fs->device, fs->mountpt, interactive);
849 if (retval) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000850 bb_error_msg("Error %d while executing fsck.%s for %s",
851 retval, type, fs->device);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000852 num_running--;
853 }
854}
855
856
857/*
858 * Deal with the fsck -t argument.
859 */
860struct fs_type_compile {
861 char **list;
862 int *type;
863 int negate;
864} fs_type_compiled;
865
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000866#define FS_TYPE_NORMAL 0
867#define FS_TYPE_OPT 1
868#define FS_TYPE_NEGOPT 2
Mike Frysinger51a43b42005-09-24 07:11:16 +0000869
870static const char *fs_type_syntax_error =
871N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
872 "with 'no' or '!'.\n");
873
874static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
875{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000876 char *cp, *list, *s;
877 int num = 2;
878 int negate, first_negate = 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000879
880 if (fs_type) {
881 for (cp=fs_type; *cp; cp++) {
882 if (*cp == ',')
883 num++;
884 }
885 }
886
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000887 cmp->list = xcalloc(num, sizeof(char *));
888 cmp->type = xcalloc(num, sizeof(int));
Mike Frysinger51a43b42005-09-24 07:11:16 +0000889 cmp->negate = 0;
890
891 if (!fs_type)
892 return;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000893
Mike Frysinger51a43b42005-09-24 07:11:16 +0000894 list = string_copy(fs_type);
895 num = 0;
896 s = strtok(list, ",");
897 while(s) {
898 negate = 0;
899 if (strncmp(s, "no", 2) == 0) {
900 s += 2;
901 negate = 1;
902 } else if (*s == '!') {
903 s++;
904 negate = 1;
905 }
906 if (strcmp(s, "loop") == 0)
907 /* loop is really short-hand for opts=loop */
908 goto loop_special_case;
909 else if (strncmp(s, "opts=", 5) == 0) {
910 s += 5;
911 loop_special_case:
912 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
913 } else {
914 if (first_negate) {
915 cmp->negate = negate;
916 first_negate = 0;
917 }
918 if ((negate && !cmp->negate) ||
919 (!negate && cmp->negate)) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000920 bb_error_msg_and_die("%s", fs_type_syntax_error);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000921 }
922 }
923#if 0
924 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
925#endif
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000926 cmp->list[num++] = string_copy(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000927 s = strtok(NULL, ",");
928 }
929 free(list);
930}
931
932/*
933 * This function returns true if a particular option appears in a
934 * comma-delimited options list
935 */
936static int opt_in_list(char *opt, char *optlist)
937{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000938 char *list, *s;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000939
940 if (!optlist)
941 return 0;
942 list = string_copy(optlist);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000943
Mike Frysinger51a43b42005-09-24 07:11:16 +0000944 s = strtok(list, ",");
945 while(s) {
946 if (strcmp(s, opt) == 0) {
947 free(list);
948 return 1;
949 }
950 s = strtok(NULL, ",");
951 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000952 free(list);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000953 return 0;
954}
955
956/* See if the filesystem matches the criteria given by the -t option */
957static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
958{
959 int n, ret = 0, checked_type = 0;
960 char *cp;
961
962 if (cmp->list == 0 || cmp->list[0] == 0)
963 return 1;
964
965 for (n=0; (cp = cmp->list[n]); n++) {
966 switch (cmp->type[n]) {
967 case FS_TYPE_NORMAL:
968 checked_type++;
969 if (strcmp(cp, fs->type) == 0) {
970 ret = 1;
971 }
972 break;
973 case FS_TYPE_NEGOPT:
974 if (opt_in_list(cp, fs->opts))
975 return 0;
976 break;
977 case FS_TYPE_OPT:
978 if (!opt_in_list(cp, fs->opts))
979 return 0;
980 break;
981 }
982 }
983 if (checked_type == 0)
984 return 1;
985 return (cmp->negate ? !ret : ret);
986}
987
988/* Check if we should ignore this filesystem. */
989static int ignore(struct fs_info *fs)
990{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000991 const char * const *ip;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000992 int wanted = 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000993 char *s;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000994
995 /*
996 * If the pass number is 0, ignore it.
997 */
998 if (fs->passno == 0)
999 return 1;
1000
1001 interpret_type(fs);
1002
1003 /*
1004 * If a specific fstype is specified, and it doesn't match,
1005 * ignore it.
1006 */
1007 if (!fs_match(fs, &fs_type_compiled)) return 1;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001008
Mike Frysinger51a43b42005-09-24 07:11:16 +00001009 /* Are we ignoring this type? */
1010 for(ip = ignored_types; *ip; ip++)
1011 if (strcmp(fs->type, *ip) == 0) return 1;
1012
1013 /* Do we really really want to check this fs? */
1014 for(ip = really_wanted; *ip; ip++)
1015 if (strcmp(fs->type, *ip) == 0) {
1016 wanted = 1;
1017 break;
1018 }
1019
1020 /* See if the <fsck.fs> program is available. */
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001021 s = find_fsck(fs->type);
1022 if (s == NULL) {
Mike Frysinger51a43b42005-09-24 07:11:16 +00001023 if (wanted)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001024 bb_error_msg("cannot check %s: fsck.%s not found",
Mike Frysinger51a43b42005-09-24 07:11:16 +00001025 fs->device, fs->type);
1026 return 1;
1027 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001028 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001029
1030 /* We can and want to check this file system type. */
1031 return 0;
1032}
1033
1034/*
1035 * Returns TRUE if a partition on the same disk is already being
1036 * checked.
1037 */
1038static int device_already_active(char *device)
1039{
1040 struct fsck_instance *inst;
1041 char *base;
1042
1043 if (force_all_parallel)
1044 return 0;
1045
1046#ifdef BASE_MD
1047 /* Don't check a soft raid disk with any other disk */
1048 if (instance_list &&
1049 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1050 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1051 return 1;
1052#endif
1053
1054 base = base_device(device);
1055 /*
1056 * If we don't know the base device, assume that the device is
1057 * already active if there are any fsck instances running.
1058 */
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001059 if (!base)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001060 return (instance_list != 0);
1061 for (inst = instance_list; inst; inst = inst->next) {
1062 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1063 free(base);
1064 return 1;
1065 }
1066 }
1067 free(base);
1068 return 0;
1069}
1070
1071/* Check all file systems, using the /etc/fstab table. */
1072static int check_all(void)
1073{
1074 struct fs_info *fs = NULL;
1075 int status = EXIT_OK;
1076 int not_done_yet = 1;
1077 int passno = 1;
1078 int pass_done;
1079
1080 if (verbose)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001081 fputs("Checking all file systems.\n", stdout);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001082
1083 /*
1084 * Do an initial scan over the filesystem; mark filesystems
1085 * which should be ignored as done, and resolve any "auto"
1086 * filesystem types (done as a side-effect of calling ignore()).
1087 */
1088 for (fs = filesys_info; fs; fs = fs->next) {
1089 if (ignore(fs))
1090 fs->flags |= FLAG_DONE;
1091 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001092
Mike Frysinger51a43b42005-09-24 07:11:16 +00001093 /*
1094 * Find and check the root filesystem.
1095 */
1096 if (!parallel_root) {
1097 for (fs = filesys_info; fs; fs = fs->next) {
1098 if (!strcmp(fs->mountpt, "/"))
1099 break;
1100 }
1101 if (fs) {
1102 if (!skip_root && !ignore(fs)) {
1103 fsck_device(fs, 1);
1104 status |= wait_many(FLAG_WAIT_ALL);
1105 if (status > EXIT_NONDESTRUCT)
1106 return status;
1107 }
1108 fs->flags |= FLAG_DONE;
1109 }
1110 }
1111 /*
1112 * This is for the bone-headed user who enters the root
1113 * filesystem twice. Skip root will skep all root entries.
1114 */
1115 if (skip_root)
1116 for (fs = filesys_info; fs; fs = fs->next)
1117 if (!strcmp(fs->mountpt, "/"))
1118 fs->flags |= FLAG_DONE;
1119
1120 while (not_done_yet) {
1121 not_done_yet = 0;
1122 pass_done = 1;
1123
1124 for (fs = filesys_info; fs; fs = fs->next) {
1125 if (cancel_requested)
1126 break;
1127 if (fs->flags & FLAG_DONE)
1128 continue;
1129 /*
1130 * If the filesystem's pass number is higher
1131 * than the current pass number, then we don't
1132 * do it yet.
1133 */
1134 if (fs->passno > passno) {
1135 not_done_yet++;
1136 continue;
1137 }
1138 /*
1139 * If a filesystem on a particular device has
1140 * already been spawned, then we need to defer
1141 * this to another pass.
1142 */
1143 if (device_already_active(fs->device)) {
1144 pass_done = 0;
1145 continue;
1146 }
1147 /*
1148 * Spawn off the fsck process
1149 */
1150 fsck_device(fs, serialize);
1151 fs->flags |= FLAG_DONE;
1152
1153 /*
1154 * Only do one filesystem at a time, or if we
1155 * have a limit on the number of fsck's extant
1156 * at one time, apply that limit.
1157 */
1158 if (serialize ||
1159 (max_running && (num_running >= max_running))) {
1160 pass_done = 0;
1161 break;
1162 }
1163 }
1164 if (cancel_requested)
1165 break;
1166 if (verbose > 1)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001167 printf("--waiting-- (pass %d)\n", passno);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001168 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1169 FLAG_WAIT_ATLEAST_ONE);
1170 if (pass_done) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001171 if (verbose > 1)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001172 printf("----------------------------------\n");
1173 passno++;
1174 } else
1175 not_done_yet++;
1176 }
1177 if (cancel_requested && !kill_sent) {
1178 kill_all(SIGTERM);
1179 kill_sent++;
1180 }
1181 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1182 return status;
1183}
1184
1185#if 0
1186static void usage(void)
1187{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001188 fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001189 exit(EXIT_USAGE);
1190}
1191#endif
1192
Mike Frysinger51a43b42005-09-24 07:11:16 +00001193static void signal_cancel(int sig FSCK_ATTR((unused)))
1194{
1195 cancel_requested++;
1196}
Mike Frysinger51a43b42005-09-24 07:11:16 +00001197
1198static void PRS(int argc, char *argv[])
1199{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001200 int i, j;
1201 char *arg, *dev, *tmp = 0;
1202 char options[128];
1203 int opt = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001204 int opts_for_fsck = 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001205 struct sigaction sa;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001206
1207 /*
1208 * Set up signal action
1209 */
1210 memset(&sa, 0, sizeof(struct sigaction));
1211 sa.sa_handler = signal_cancel;
1212 sigaction(SIGINT, &sa, 0);
1213 sigaction(SIGTERM, &sa, 0);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001214
Mike Frysinger51a43b42005-09-24 07:11:16 +00001215 num_devices = 0;
1216 num_args = 0;
1217 instance_list = 0;
1218
Mike Frysinger51a43b42005-09-24 07:11:16 +00001219 for (i=1; i < argc; i++) {
1220 arg = argv[i];
1221 if (!arg)
1222 continue;
1223 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1224 if (num_devices >= MAX_DEVICES) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001225 bb_error_msg_and_die("too many devices");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001226 }
1227 dev = blkid_get_devname(cache, arg, NULL);
1228 if (!dev && strchr(arg, '=')) {
1229 /*
1230 * Check to see if we failed because
1231 * /proc/partitions isn't found.
1232 */
1233 if (access("/proc/partitions", R_OK) < 0) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001234 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1235 "Is /proc mounted?");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001236 }
1237 /*
1238 * Check to see if this is because
1239 * we're not running as root
1240 */
1241 if (geteuid())
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001242 bb_error_msg_and_die(
Mike Frysinger51a43b42005-09-24 07:11:16 +00001243 "Must be root to scan for matching filesystems: %s\n", arg);
1244 else
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001245 bb_error_msg_and_die(
1246 "Couldn't find matching filesystem: %s", arg);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001247 }
1248 devices[num_devices++] = dev ? dev : string_copy(arg);
1249 continue;
1250 }
1251 if (arg[0] != '-' || opts_for_fsck) {
1252 if (num_args >= MAX_ARGS) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001253 bb_error_msg_and_die("too many arguments");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001254 }
1255 args[num_args++] = string_copy(arg);
1256 continue;
1257 }
1258 for (j=1; arg[j]; j++) {
1259 if (opts_for_fsck) {
1260 options[++opt] = arg[j];
1261 continue;
1262 }
1263 switch (arg[j]) {
1264 case 'A':
1265 doall++;
1266 break;
1267 case 'C':
1268 progress++;
1269 if (arg[j+1]) {
1270 progress_fd = string_to_int(arg+j+1);
1271 if (progress_fd < 0)
1272 progress_fd = 0;
1273 else
1274 goto next_arg;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001275 } else if ((i+1) < argc &&
Mike Frysinger51a43b42005-09-24 07:11:16 +00001276 !strncmp(argv[i+1], "-", 1) == 0) {
1277 progress_fd = string_to_int(argv[i]);
1278 if (progress_fd < 0)
1279 progress_fd = 0;
1280 else {
1281 goto next_arg;
1282 i++;
1283 }
1284 }
1285 break;
1286 case 'V':
1287 verbose++;
1288 break;
1289 case 'N':
1290 noexecute++;
1291 break;
1292 case 'R':
1293 skip_root++;
1294 break;
1295 case 'T':
1296 notitle++;
1297 break;
1298 case 'M':
1299 like_mount++;
1300 break;
1301 case 'P':
1302 parallel_root++;
1303 break;
1304 case 's':
1305 serialize++;
1306 break;
1307 case 't':
1308 tmp = 0;
1309 if (fstype)
1310 usage();
1311 if (arg[j+1])
1312 tmp = arg+j+1;
1313 else if ((i+1) < argc)
1314 tmp = argv[++i];
1315 else
1316 usage();
1317 fstype = string_copy(tmp);
1318 compile_fs_type(fstype, &fs_type_compiled);
1319 goto next_arg;
1320 case '-':
1321 opts_for_fsck++;
1322 break;
1323 case '?':
1324 usage();
1325 break;
1326 default:
1327 options[++opt] = arg[j];
1328 break;
1329 }
1330 }
1331 next_arg:
1332 if (opt) {
1333 options[0] = '-';
1334 options[++opt] = '\0';
1335 if (num_args >= MAX_ARGS) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001336 bb_error_msg("too many arguments");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001337 }
1338 args[num_args++] = string_copy(options);
1339 opt = 0;
1340 }
1341 }
1342 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1343 force_all_parallel++;
1344 if ((tmp = getenv("FSCK_MAX_INST")))
1345 max_running = atoi(tmp);
1346}
1347
1348int fsck_main(int argc, char *argv[])
1349{
1350 int i, status = 0;
1351 int interactive = 0;
1352 char *oldpath = getenv("PATH");
1353 const char *fstab;
1354 struct fs_info *fs;
1355
1356 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1357 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1358
Mike Frysinger51a43b42005-09-24 07:11:16 +00001359 blkid_get_cache(&cache, NULL);
1360 PRS(argc, argv);
1361
1362 if (!notitle)
1363 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1364
1365 fstab = getenv("FSTAB_FILE");
1366 if (!fstab)
1367 fstab = _PATH_MNTTAB;
1368 load_fs_info(fstab);
1369
1370 /* Update our search path to include uncommon directories. */
1371 if (oldpath) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +00001372 fsck_path = bb_xasprintf("%s:%s", fsck_prefix_path, oldpath);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001373 } else {
1374 fsck_path = string_copy(fsck_prefix_path);
1375 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001376
Mike Frysinger51a43b42005-09-24 07:11:16 +00001377 if ((num_devices == 1) || (serialize))
1378 interactive = 1;
1379
1380 /* If -A was specified ("check all"), do that! */
1381 if (doall)
1382 return check_all();
1383
1384 if (num_devices == 0) {
1385 serialize++;
1386 interactive++;
1387 return check_all();
1388 }
1389 for (i = 0 ; i < num_devices; i++) {
1390 if (cancel_requested) {
1391 if (!kill_sent) {
1392 kill_all(SIGTERM);
1393 kill_sent++;
1394 }
1395 break;
1396 }
1397 fs = lookup(devices[i]);
1398 if (!fs) {
1399 fs = create_fs_device(devices[i], 0, "auto",
1400 0, -1, -1);
1401 if (!fs)
1402 continue;
1403 }
1404 fsck_device(fs, interactive);
1405 if (serialize ||
1406 (max_running && (num_running >= max_running))) {
1407 struct fsck_instance *inst;
1408
1409 inst = wait_one(0);
1410 if (inst) {
1411 status |= inst->exit_status;
1412 free_instance(inst);
1413 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001414 if (verbose > 1)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001415 printf("----------------------------------\n");
1416 }
1417 }
1418 status |= wait_many(FLAG_WAIT_ALL);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001419 if (ENABLE_FEATURE_CLEAN_UP)
1420 free(fsck_path);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001421 blkid_put_cache(cache);
1422 return status;
1423}