[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * pfsck --- 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 |
|
---|
| 29 | #include <sys/types.h>
|
---|
| 30 | #include <sys/wait.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 <signal.h>
|
---|
| 43 |
|
---|
| 44 | #include "fsck.h"
|
---|
| 45 | #include "blkid/blkid.h"
|
---|
| 46 |
|
---|
| 47 | #include "e2fsbb.h"
|
---|
| 48 |
|
---|
| 49 | #include "libbb.h"
|
---|
| 50 |
|
---|
| 51 | #ifndef _PATH_MNTTAB
|
---|
| 52 | #define _PATH_MNTTAB "/etc/fstab"
|
---|
| 53 | #endif
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | * 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 |
|
---|
| 70 | struct 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 | */
|
---|
| 87 | struct 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 | /*
|
---|
| 100 | * 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 cannot be determined, return NULL.
|
---|
| 106 | *
|
---|
| 107 | * The base_device() function returns an allocated string which must
|
---|
| 108 | * be freed.
|
---|
| 109 | *
|
---|
| 110 | */
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | #ifdef CONFIG_FEATURE_DEVFS
|
---|
| 114 | /*
|
---|
| 115 | * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
|
---|
| 116 | * pathames.
|
---|
| 117 | */
|
---|
| 118 | static const char *const devfs_hier[] = {
|
---|
| 119 | "host", "bus", "target", "lun", 0
|
---|
| 120 | };
|
---|
| 121 | #endif
|
---|
| 122 |
|
---|
| 123 | static char *base_device(const char *device)
|
---|
| 124 | {
|
---|
| 125 | char *str, *cp;
|
---|
| 126 | #ifdef CONFIG_FEATURE_DEVFS
|
---|
| 127 | const char *const *hier;
|
---|
| 128 | const char *disk;
|
---|
| 129 | int len;
|
---|
| 130 | #endif
|
---|
| 131 |
|
---|
| 132 | cp = str = 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 | /*
|
---|
| 140 | * For md devices, we treat them all as if they were all
|
---|
| 141 | * on one disk, since we don't know how to parallelize them.
|
---|
| 142 | */
|
---|
| 143 | if (cp[0] == 'm' && cp[1] == 'd') {
|
---|
| 144 | *(cp+2) = 0;
|
---|
| 145 | return str;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /* Handle DAC 960 devices */
|
---|
| 149 | if (strncmp(cp, "rd/", 3) == 0) {
|
---|
| 150 | cp += 3;
|
---|
| 151 | if (cp[0] != 'c' || cp[2] != 'd' ||
|
---|
| 152 | !isdigit(cp[1]) || !isdigit(cp[3]))
|
---|
| 153 | goto errout;
|
---|
| 154 | *(cp+4) = 0;
|
---|
| 155 | return str;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /* Now let's handle /dev/hd* and /dev/sd* devices.... */
|
---|
| 159 | if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
|
---|
| 160 | cp += 2;
|
---|
| 161 | /* If there's a single number after /dev/hd, skip it */
|
---|
| 162 | if (isdigit(*cp))
|
---|
| 163 | cp++;
|
---|
| 164 | /* What follows must be an alpha char, or give up */
|
---|
| 165 | if (!isalpha(*cp))
|
---|
| 166 | goto errout;
|
---|
| 167 | *(cp + 1) = 0;
|
---|
| 168 | return str;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | #ifdef CONFIG_FEATURE_DEVFS
|
---|
| 172 | /* Now let's handle devfs (ugh) names */
|
---|
| 173 | len = 0;
|
---|
| 174 | if (strncmp(cp, "ide/", 4) == 0)
|
---|
| 175 | len = 4;
|
---|
| 176 | if (strncmp(cp, "scsi/", 5) == 0)
|
---|
| 177 | len = 5;
|
---|
| 178 | if (len) {
|
---|
| 179 | cp += len;
|
---|
| 180 | /*
|
---|
| 181 | * Now we proceed down the expected devfs hierarchy.
|
---|
| 182 | * i.e., .../host1/bus2/target3/lun4/...
|
---|
| 183 | * If we don't find the expected token, followed by
|
---|
| 184 | * some number of digits at each level, abort.
|
---|
| 185 | */
|
---|
| 186 | for (hier = devfs_hier; *hier; hier++) {
|
---|
| 187 | len = strlen(*hier);
|
---|
| 188 | if (strncmp(cp, *hier, len) != 0)
|
---|
| 189 | goto errout;
|
---|
| 190 | cp += len;
|
---|
| 191 | while (*cp != '/' && *cp != 0) {
|
---|
| 192 | if (!isdigit(*cp))
|
---|
| 193 | goto errout;
|
---|
| 194 | cp++;
|
---|
| 195 | }
|
---|
| 196 | cp++;
|
---|
| 197 | }
|
---|
| 198 | *(cp - 1) = 0;
|
---|
| 199 | return str;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /* Now handle devfs /dev/disc or /dev/disk names */
|
---|
| 203 | disk = 0;
|
---|
| 204 | if (strncmp(cp, "discs/", 6) == 0)
|
---|
| 205 | disk = "disc";
|
---|
| 206 | else if (strncmp(cp, "disks/", 6) == 0)
|
---|
| 207 | disk = "disk";
|
---|
| 208 | if (disk) {
|
---|
| 209 | cp += 6;
|
---|
| 210 | if (strncmp(cp, disk, 4) != 0)
|
---|
| 211 | goto errout;
|
---|
| 212 | cp += 4;
|
---|
| 213 | while (*cp != '/' && *cp != 0) {
|
---|
| 214 | if (!isdigit(*cp))
|
---|
| 215 | goto errout;
|
---|
| 216 | cp++;
|
---|
| 217 | }
|
---|
| 218 | *cp = 0;
|
---|
| 219 | return str;
|
---|
| 220 | }
|
---|
| 221 | #endif
|
---|
| 222 |
|
---|
| 223 | errout:
|
---|
| 224 | free(str);
|
---|
| 225 | return NULL;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | static const char *const ignored_types[] = {
|
---|
| 230 | "ignore",
|
---|
| 231 | "iso9660",
|
---|
| 232 | "nfs",
|
---|
| 233 | "proc",
|
---|
| 234 | "sw",
|
---|
| 235 | "swap",
|
---|
| 236 | "tmpfs",
|
---|
| 237 | "devpts",
|
---|
| 238 | NULL
|
---|
| 239 | };
|
---|
| 240 |
|
---|
| 241 | static const char *const really_wanted[] = {
|
---|
| 242 | "minix",
|
---|
| 243 | "ext2",
|
---|
| 244 | "ext3",
|
---|
| 245 | "jfs",
|
---|
| 246 | "reiserfs",
|
---|
| 247 | "xiafs",
|
---|
| 248 | "xfs",
|
---|
| 249 | NULL
|
---|
| 250 | };
|
---|
| 251 |
|
---|
| 252 | #define BASE_MD "/dev/md"
|
---|
| 253 |
|
---|
| 254 | /*
|
---|
| 255 | * Global variables for options
|
---|
| 256 | */
|
---|
| 257 | static char *devices[MAX_DEVICES];
|
---|
| 258 | static char *args[MAX_ARGS];
|
---|
| 259 | static int num_devices, num_args;
|
---|
| 260 |
|
---|
| 261 | static int verbose;
|
---|
| 262 | static int doall;
|
---|
| 263 | static int noexecute;
|
---|
| 264 | static int serialize;
|
---|
| 265 | static int skip_root;
|
---|
| 266 | static int like_mount;
|
---|
| 267 | static int notitle;
|
---|
| 268 | static int parallel_root;
|
---|
| 269 | static int progress;
|
---|
| 270 | static int progress_fd;
|
---|
| 271 | static int force_all_parallel;
|
---|
| 272 | static int num_running;
|
---|
| 273 | static int max_running;
|
---|
| 274 | static volatile int cancel_requested;
|
---|
| 275 | static int kill_sent;
|
---|
| 276 | static char *fstype;
|
---|
| 277 | static struct fs_info *filesys_info, *filesys_last;
|
---|
| 278 | static struct fsck_instance *instance_list;
|
---|
| 279 | static char *fsck_path;
|
---|
| 280 | static blkid_cache cache;
|
---|
| 281 |
|
---|
| 282 | static char *string_copy(const char *s)
|
---|
| 283 | {
|
---|
| 284 | char *ret;
|
---|
| 285 |
|
---|
| 286 | if (!s)
|
---|
| 287 | return 0;
|
---|
| 288 | ret = strdup(s);
|
---|
| 289 | return ret;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | static int string_to_int(const char *s)
|
---|
| 293 | {
|
---|
| 294 | long l;
|
---|
| 295 | char *p;
|
---|
| 296 |
|
---|
| 297 | l = strtol(s, &p, 0);
|
---|
| 298 | if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
|
---|
| 299 | return -1;
|
---|
| 300 | else
|
---|
| 301 | return (int) l;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | static char *skip_over_blank(char *cp)
|
---|
| 305 | {
|
---|
| 306 | while (*cp && isspace(*cp))
|
---|
| 307 | cp++;
|
---|
| 308 | return cp;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | static char *skip_over_word(char *cp)
|
---|
| 312 | {
|
---|
| 313 | while (*cp && !isspace(*cp))
|
---|
| 314 | cp++;
|
---|
| 315 | return cp;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | static void strip_line(char *line)
|
---|
| 319 | {
|
---|
| 320 | char *p;
|
---|
| 321 |
|
---|
| 322 | while (*line) {
|
---|
| 323 | p = line + strlen(line) - 1;
|
---|
| 324 | if ((*p == '\n') || (*p == '\r'))
|
---|
| 325 | *p = 0;
|
---|
| 326 | else
|
---|
| 327 | break;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | static char *parse_word(char **buf)
|
---|
| 332 | {
|
---|
| 333 | char *word, *next;
|
---|
| 334 |
|
---|
| 335 | word = *buf;
|
---|
| 336 | if (*word == 0)
|
---|
| 337 | return 0;
|
---|
| 338 |
|
---|
| 339 | word = skip_over_blank(word);
|
---|
| 340 | next = skip_over_word(word);
|
---|
| 341 | if (*next)
|
---|
| 342 | *next++ = 0;
|
---|
| 343 | *buf = next;
|
---|
| 344 | return word;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | static void parse_escape(char *word)
|
---|
| 348 | {
|
---|
| 349 | char *q, c;
|
---|
| 350 | const char *p;
|
---|
| 351 |
|
---|
| 352 | if (!word)
|
---|
| 353 | return;
|
---|
| 354 |
|
---|
| 355 | for (p = q = word; *p; q++) {
|
---|
| 356 | c = *p++;
|
---|
| 357 | if (c != '\\') {
|
---|
| 358 | *q = c;
|
---|
| 359 | } else {
|
---|
| 360 | *q = bb_process_escape_sequence(&p);
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | *q = 0;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | static void free_instance(struct fsck_instance *i)
|
---|
| 367 | {
|
---|
| 368 | if (i->prog)
|
---|
| 369 | free(i->prog);
|
---|
| 370 | if (i->device)
|
---|
| 371 | free(i->device);
|
---|
| 372 | if (i->base_device)
|
---|
| 373 | free(i->base_device);
|
---|
| 374 | free(i);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
|
---|
| 378 | const char *type, const char *opts,
|
---|
| 379 | int freq, int passno)
|
---|
| 380 | {
|
---|
| 381 | struct fs_info *fs;
|
---|
| 382 |
|
---|
| 383 | if (!(fs = malloc(sizeof(struct fs_info))))
|
---|
| 384 | return NULL;
|
---|
| 385 |
|
---|
| 386 | fs->device = string_copy(device);
|
---|
| 387 | fs->mountpt = string_copy(mntpnt);
|
---|
| 388 | fs->type = string_copy(type);
|
---|
| 389 | fs->opts = string_copy(opts ? opts : "");
|
---|
| 390 | fs->freq = freq;
|
---|
| 391 | fs->passno = passno;
|
---|
| 392 | fs->flags = 0;
|
---|
| 393 | fs->next = NULL;
|
---|
| 394 |
|
---|
| 395 | if (!filesys_info)
|
---|
| 396 | filesys_info = fs;
|
---|
| 397 | else
|
---|
| 398 | filesys_last->next = fs;
|
---|
| 399 | filesys_last = fs;
|
---|
| 400 |
|
---|
| 401 | return fs;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 |
|
---|
| 406 | static int parse_fstab_line(char *line, struct fs_info **ret_fs)
|
---|
| 407 | {
|
---|
| 408 | char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
|
---|
| 409 | struct fs_info *fs;
|
---|
| 410 |
|
---|
| 411 | *ret_fs = 0;
|
---|
| 412 | strip_line(line);
|
---|
| 413 | if ((cp = strchr(line, '#')))
|
---|
| 414 | *cp = 0; /* Ignore everything after the comment char */
|
---|
| 415 | cp = line;
|
---|
| 416 |
|
---|
| 417 | device = parse_word(&cp);
|
---|
| 418 | mntpnt = parse_word(&cp);
|
---|
| 419 | type = parse_word(&cp);
|
---|
| 420 | opts = parse_word(&cp);
|
---|
| 421 | freq = parse_word(&cp);
|
---|
| 422 | passno = parse_word(&cp);
|
---|
| 423 |
|
---|
| 424 | if (!device)
|
---|
| 425 | return 0; /* Allow blank lines */
|
---|
| 426 |
|
---|
| 427 | if (!mntpnt || !type)
|
---|
| 428 | return -1;
|
---|
| 429 |
|
---|
| 430 | parse_escape(device);
|
---|
| 431 | parse_escape(mntpnt);
|
---|
| 432 | parse_escape(type);
|
---|
| 433 | parse_escape(opts);
|
---|
| 434 | parse_escape(freq);
|
---|
| 435 | parse_escape(passno);
|
---|
| 436 |
|
---|
| 437 | dev = blkid_get_devname(cache, device, NULL);
|
---|
| 438 | if (dev)
|
---|
| 439 | device = dev;
|
---|
| 440 |
|
---|
| 441 | if (strchr(type, ','))
|
---|
| 442 | type = 0;
|
---|
| 443 |
|
---|
| 444 | fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
|
---|
| 445 | freq ? atoi(freq) : -1,
|
---|
| 446 | passno ? atoi(passno) : -1);
|
---|
| 447 | if (dev)
|
---|
| 448 | free(dev);
|
---|
| 449 |
|
---|
| 450 | if (!fs)
|
---|
| 451 | return -1;
|
---|
| 452 | *ret_fs = fs;
|
---|
| 453 | return 0;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | static void interpret_type(struct fs_info *fs)
|
---|
| 457 | {
|
---|
| 458 | char *t;
|
---|
| 459 |
|
---|
| 460 | if (strcmp(fs->type, "auto") != 0)
|
---|
| 461 | return;
|
---|
| 462 | t = blkid_get_tag_value(cache, "TYPE", fs->device);
|
---|
| 463 | if (t) {
|
---|
| 464 | free(fs->type);
|
---|
| 465 | fs->type = t;
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | /*
|
---|
| 470 | * Load the filesystem database from /etc/fstab
|
---|
| 471 | */
|
---|
| 472 | static void load_fs_info(const char *filename)
|
---|
| 473 | {
|
---|
| 474 | FILE *f;
|
---|
| 475 | char buf[1024];
|
---|
| 476 | int lineno = 0;
|
---|
| 477 | int old_fstab = 1;
|
---|
| 478 | struct fs_info *fs;
|
---|
| 479 |
|
---|
| 480 | if ((f = fopen(filename, "r")) == NULL) {
|
---|
| 481 | bb_perror_msg("WARNING: cannot open %s", filename);
|
---|
| 482 | return;
|
---|
| 483 | }
|
---|
| 484 | while (!feof(f)) {
|
---|
| 485 | lineno++;
|
---|
| 486 | if (!fgets(buf, sizeof(buf), f))
|
---|
| 487 | break;
|
---|
| 488 | buf[sizeof(buf)-1] = 0;
|
---|
| 489 | if (parse_fstab_line(buf, &fs) < 0) {
|
---|
| 490 | bb_error_msg("WARNING: bad format "
|
---|
| 491 | "on line %d of %s\n", lineno, filename);
|
---|
| 492 | continue;
|
---|
| 493 | }
|
---|
| 494 | if (!fs)
|
---|
| 495 | continue;
|
---|
| 496 | if (fs->passno < 0)
|
---|
| 497 | fs->passno = 0;
|
---|
| 498 | else
|
---|
| 499 | old_fstab = 0;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | fclose(f);
|
---|
| 503 |
|
---|
| 504 | if (old_fstab) {
|
---|
| 505 | fputs("\007\007\007"
|
---|
| 506 | "WARNING: Your /etc/fstab does not contain the fsck passno\n"
|
---|
| 507 | " field. I will kludge around things for you, but you\n"
|
---|
| 508 | " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
|
---|
| 509 |
|
---|
| 510 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 511 | fs->passno = 1;
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | /* Lookup filesys in /etc/fstab and return the corresponding entry. */
|
---|
| 517 | static struct fs_info *lookup(char *filesys)
|
---|
| 518 | {
|
---|
| 519 | struct fs_info *fs;
|
---|
| 520 |
|
---|
| 521 | /* No filesys name given. */
|
---|
| 522 | if (filesys == NULL)
|
---|
| 523 | return NULL;
|
---|
| 524 |
|
---|
| 525 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 526 | if (!strcmp(filesys, fs->device) ||
|
---|
| 527 | (fs->mountpt && !strcmp(filesys, fs->mountpt)))
|
---|
| 528 | break;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | return fs;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | /* Find fsck program for a given fs type. */
|
---|
| 535 | static char *find_fsck(char *type)
|
---|
| 536 | {
|
---|
| 537 | char *s;
|
---|
| 538 | const char *tpl;
|
---|
| 539 | char *p = string_copy(fsck_path);
|
---|
| 540 | struct stat st;
|
---|
| 541 |
|
---|
| 542 | /* Are we looking for a program or just a type? */
|
---|
| 543 | tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
|
---|
| 544 |
|
---|
| 545 | for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
|
---|
| 546 | s = xasprintf(tpl, s, type);
|
---|
| 547 | if (stat(s, &st) == 0) break;
|
---|
| 548 | free(s);
|
---|
| 549 | }
|
---|
| 550 | free(p);
|
---|
| 551 | return s;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | static int progress_active(void)
|
---|
| 555 | {
|
---|
| 556 | struct fsck_instance *inst;
|
---|
| 557 |
|
---|
| 558 | for (inst = instance_list; inst; inst = inst->next) {
|
---|
| 559 | if (inst->flags & FLAG_DONE)
|
---|
| 560 | continue;
|
---|
| 561 | if (inst->flags & FLAG_PROGRESS)
|
---|
| 562 | return 1;
|
---|
| 563 | }
|
---|
| 564 | return 0;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | /*
|
---|
| 568 | * Execute a particular fsck program, and link it into the list of
|
---|
| 569 | * child processes we are waiting for.
|
---|
| 570 | */
|
---|
| 571 | static int execute(const char *type, const char *device, const char *mntpt,
|
---|
| 572 | int interactive)
|
---|
| 573 | {
|
---|
| 574 | char *s, *argv[80];
|
---|
| 575 | char *prog;
|
---|
| 576 | int argc, i;
|
---|
| 577 | struct fsck_instance *inst, *p;
|
---|
| 578 | pid_t pid;
|
---|
| 579 |
|
---|
| 580 | inst = malloc(sizeof(struct fsck_instance));
|
---|
| 581 | if (!inst)
|
---|
| 582 | return ENOMEM;
|
---|
| 583 | memset(inst, 0, sizeof(struct fsck_instance));
|
---|
| 584 |
|
---|
| 585 | prog = xasprintf("fsck.%s", type);
|
---|
| 586 | argv[0] = prog;
|
---|
| 587 | argc = 1;
|
---|
| 588 |
|
---|
| 589 | for (i=0; i <num_args; i++)
|
---|
| 590 | argv[argc++] = string_copy(args[i]);
|
---|
| 591 |
|
---|
| 592 | if (progress && !progress_active()) {
|
---|
| 593 | if ((strcmp(type, "ext2") == 0) ||
|
---|
| 594 | (strcmp(type, "ext3") == 0)) {
|
---|
| 595 | char tmp[80];
|
---|
| 596 | snprintf(tmp, 80, "-C%d", progress_fd);
|
---|
| 597 | argv[argc++] = string_copy(tmp);
|
---|
| 598 | inst->flags |= FLAG_PROGRESS;
|
---|
| 599 | }
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | argv[argc++] = string_copy(device);
|
---|
| 603 | argv[argc] = 0;
|
---|
| 604 |
|
---|
| 605 | s = find_fsck(prog);
|
---|
| 606 | if (s == NULL) {
|
---|
| 607 | bb_error_msg("%s: not found", prog);
|
---|
| 608 | return ENOENT;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | if (verbose || noexecute) {
|
---|
| 612 | printf("[%s (%d) -- %s] ", s, num_running,
|
---|
| 613 | mntpt ? mntpt : device);
|
---|
| 614 | for (i=0; i < argc; i++)
|
---|
| 615 | printf("%s ", argv[i]);
|
---|
| 616 | puts("");
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | /* Fork and execute the correct program. */
|
---|
| 620 | if (noexecute)
|
---|
| 621 | pid = -1;
|
---|
| 622 | else if ((pid = fork()) < 0) {
|
---|
| 623 | perror("fork");
|
---|
| 624 | return errno;
|
---|
| 625 | } else if (pid == 0) {
|
---|
| 626 | if (!interactive)
|
---|
| 627 | close(0);
|
---|
| 628 | (void) execv(s, argv);
|
---|
| 629 | bb_perror_msg_and_die("%s", argv[0]);
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | for (i = 1; i < argc; i++)
|
---|
| 633 | free(argv[i]);
|
---|
| 634 |
|
---|
| 635 | free(s);
|
---|
| 636 | inst->pid = pid;
|
---|
| 637 | inst->prog = prog;
|
---|
| 638 | inst->type = string_copy(type);
|
---|
| 639 | inst->device = string_copy(device);
|
---|
| 640 | inst->base_device = base_device(device);
|
---|
| 641 | inst->start_time = time(0);
|
---|
| 642 | inst->next = NULL;
|
---|
| 643 |
|
---|
| 644 | /*
|
---|
| 645 | * Find the end of the list, so we add the instance on at the end.
|
---|
| 646 | */
|
---|
| 647 | for (p = instance_list; p && p->next; p = p->next);
|
---|
| 648 |
|
---|
| 649 | if (p)
|
---|
| 650 | p->next = inst;
|
---|
| 651 | else
|
---|
| 652 | instance_list = inst;
|
---|
| 653 |
|
---|
| 654 | return 0;
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | /*
|
---|
| 658 | * Send a signal to all outstanding fsck child processes
|
---|
| 659 | */
|
---|
| 660 | static int kill_all(int signum)
|
---|
| 661 | {
|
---|
| 662 | struct fsck_instance *inst;
|
---|
| 663 | int n = 0;
|
---|
| 664 |
|
---|
| 665 | for (inst = instance_list; inst; inst = inst->next) {
|
---|
| 666 | if (inst->flags & FLAG_DONE)
|
---|
| 667 | continue;
|
---|
| 668 | kill(inst->pid, signum);
|
---|
| 669 | n++;
|
---|
| 670 | }
|
---|
| 671 | return n;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | /*
|
---|
| 675 | * Wait for one child process to exit; when it does, unlink it from
|
---|
| 676 | * the list of executing child processes, and return it.
|
---|
| 677 | */
|
---|
| 678 | static struct fsck_instance *wait_one(int flags)
|
---|
| 679 | {
|
---|
| 680 | int status;
|
---|
| 681 | int sig;
|
---|
| 682 | struct fsck_instance *inst, *inst2, *prev;
|
---|
| 683 | pid_t pid;
|
---|
| 684 |
|
---|
| 685 | if (!instance_list)
|
---|
| 686 | return NULL;
|
---|
| 687 |
|
---|
| 688 | if (noexecute) {
|
---|
| 689 | inst = instance_list;
|
---|
| 690 | prev = 0;
|
---|
| 691 | #ifdef RANDOM_DEBUG
|
---|
| 692 | while (inst->next && (random() & 1)) {
|
---|
| 693 | prev = inst;
|
---|
| 694 | inst = inst->next;
|
---|
| 695 | }
|
---|
| 696 | #endif
|
---|
| 697 | inst->exit_status = 0;
|
---|
| 698 | goto ret_inst;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | /*
|
---|
| 702 | * gcc -Wall fails saving throw against stupidity
|
---|
| 703 | * (inst and prev are thought to be uninitialized variables)
|
---|
| 704 | */
|
---|
| 705 | inst = prev = NULL;
|
---|
| 706 |
|
---|
| 707 | do {
|
---|
| 708 | pid = waitpid(-1, &status, flags);
|
---|
| 709 | if (cancel_requested && !kill_sent) {
|
---|
| 710 | kill_all(SIGTERM);
|
---|
| 711 | kill_sent++;
|
---|
| 712 | }
|
---|
| 713 | if ((pid == 0) && (flags & WNOHANG))
|
---|
| 714 | return NULL;
|
---|
| 715 | if (pid < 0) {
|
---|
| 716 | if ((errno == EINTR) || (errno == EAGAIN))
|
---|
| 717 | continue;
|
---|
| 718 | if (errno == ECHILD) {
|
---|
| 719 | bb_error_msg("wait: no more child process?!?");
|
---|
| 720 | return NULL;
|
---|
| 721 | }
|
---|
| 722 | perror("wait");
|
---|
| 723 | continue;
|
---|
| 724 | }
|
---|
| 725 | for (prev = 0, inst = instance_list;
|
---|
| 726 | inst;
|
---|
| 727 | prev = inst, inst = inst->next) {
|
---|
| 728 | if (inst->pid == pid)
|
---|
| 729 | break;
|
---|
| 730 | }
|
---|
| 731 | } while (!inst);
|
---|
| 732 |
|
---|
| 733 | if (WIFEXITED(status))
|
---|
| 734 | status = WEXITSTATUS(status);
|
---|
| 735 | else if (WIFSIGNALED(status)) {
|
---|
| 736 | sig = WTERMSIG(status);
|
---|
| 737 | if (sig == SIGINT) {
|
---|
| 738 | status = EXIT_UNCORRECTED;
|
---|
| 739 | } else {
|
---|
| 740 | printf("Warning... %s for device %s exited "
|
---|
| 741 | "with signal %d.\n",
|
---|
| 742 | inst->prog, inst->device, sig);
|
---|
| 743 | status = EXIT_ERROR;
|
---|
| 744 | }
|
---|
| 745 | } else {
|
---|
| 746 | printf("%s %s: status is %x, should never happen.\n",
|
---|
| 747 | inst->prog, inst->device, status);
|
---|
| 748 | status = EXIT_ERROR;
|
---|
| 749 | }
|
---|
| 750 | inst->exit_status = status;
|
---|
| 751 | if (progress && (inst->flags & FLAG_PROGRESS) &&
|
---|
| 752 | !progress_active()) {
|
---|
| 753 | for (inst2 = instance_list; inst2; inst2 = inst2->next) {
|
---|
| 754 | if (inst2->flags & FLAG_DONE)
|
---|
| 755 | continue;
|
---|
| 756 | if (strcmp(inst2->type, "ext2") &&
|
---|
| 757 | strcmp(inst2->type, "ext3"))
|
---|
| 758 | continue;
|
---|
| 759 | /*
|
---|
| 760 | * If we've just started the fsck, wait a tiny
|
---|
| 761 | * bit before sending the kill, to give it
|
---|
| 762 | * time to set up the signal handler
|
---|
| 763 | */
|
---|
| 764 | if (inst2->start_time < time(0)+2) {
|
---|
| 765 | if (fork() == 0) {
|
---|
| 766 | sleep(1);
|
---|
| 767 | kill(inst2->pid, SIGUSR1);
|
---|
| 768 | exit(0);
|
---|
| 769 | }
|
---|
| 770 | } else
|
---|
| 771 | kill(inst2->pid, SIGUSR1);
|
---|
| 772 | inst2->flags |= FLAG_PROGRESS;
|
---|
| 773 | break;
|
---|
| 774 | }
|
---|
| 775 | }
|
---|
| 776 | ret_inst:
|
---|
| 777 | if (prev)
|
---|
| 778 | prev->next = inst->next;
|
---|
| 779 | else
|
---|
| 780 | instance_list = inst->next;
|
---|
| 781 | if (verbose > 1)
|
---|
| 782 | printf("Finished with %s (exit status %d)\n",
|
---|
| 783 | inst->device, inst->exit_status);
|
---|
| 784 | num_running--;
|
---|
| 785 | return inst;
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | #define FLAG_WAIT_ALL 0
|
---|
| 789 | #define FLAG_WAIT_ATLEAST_ONE 1
|
---|
| 790 | /*
|
---|
| 791 | * Wait until all executing child processes have exited; return the
|
---|
| 792 | * logical OR of all of their exit code values.
|
---|
| 793 | */
|
---|
| 794 | static int wait_many(int flags)
|
---|
| 795 | {
|
---|
| 796 | struct fsck_instance *inst;
|
---|
| 797 | int global_status = 0;
|
---|
| 798 | int wait_flags = 0;
|
---|
| 799 |
|
---|
| 800 | while ((inst = wait_one(wait_flags))) {
|
---|
| 801 | global_status |= inst->exit_status;
|
---|
| 802 | free_instance(inst);
|
---|
| 803 | #ifdef RANDOM_DEBUG
|
---|
| 804 | if (noexecute && (flags & WNOHANG) && !(random() % 3))
|
---|
| 805 | break;
|
---|
| 806 | #endif
|
---|
| 807 | if (flags & FLAG_WAIT_ATLEAST_ONE)
|
---|
| 808 | wait_flags = WNOHANG;
|
---|
| 809 | }
|
---|
| 810 | return global_status;
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | /*
|
---|
| 814 | * Run the fsck program on a particular device
|
---|
| 815 | *
|
---|
| 816 | * If the type is specified using -t, and it isn't prefixed with "no"
|
---|
| 817 | * (as in "noext2") and only one filesystem type is specified, then
|
---|
| 818 | * use that type regardless of what is specified in /etc/fstab.
|
---|
| 819 | *
|
---|
| 820 | * If the type isn't specified by the user, then use either the type
|
---|
| 821 | * specified in /etc/fstab, or DEFAULT_FSTYPE.
|
---|
| 822 | */
|
---|
| 823 | static void fsck_device(struct fs_info *fs, int interactive)
|
---|
| 824 | {
|
---|
| 825 | const char *type;
|
---|
| 826 | int retval;
|
---|
| 827 |
|
---|
| 828 | interpret_type(fs);
|
---|
| 829 |
|
---|
| 830 | if (strcmp(fs->type, "auto") != 0)
|
---|
| 831 | type = fs->type;
|
---|
| 832 | else if (fstype && strncmp(fstype, "no", 2) &&
|
---|
| 833 | strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
|
---|
| 834 | !strchr(fstype, ','))
|
---|
| 835 | type = fstype;
|
---|
| 836 | else
|
---|
| 837 | type = DEFAULT_FSTYPE;
|
---|
| 838 |
|
---|
| 839 | num_running++;
|
---|
| 840 | retval = execute(type, fs->device, fs->mountpt, interactive);
|
---|
| 841 | if (retval) {
|
---|
| 842 | bb_error_msg("error %d while executing fsck.%s for %s",
|
---|
| 843 | retval, type, fs->device);
|
---|
| 844 | num_running--;
|
---|
| 845 | }
|
---|
| 846 | }
|
---|
| 847 |
|
---|
| 848 |
|
---|
| 849 | /*
|
---|
| 850 | * Deal with the fsck -t argument.
|
---|
| 851 | */
|
---|
| 852 | struct fs_type_compile {
|
---|
| 853 | char **list;
|
---|
| 854 | int *type;
|
---|
| 855 | int negate;
|
---|
| 856 | } fs_type_compiled;
|
---|
| 857 |
|
---|
| 858 | #define FS_TYPE_NORMAL 0
|
---|
| 859 | #define FS_TYPE_OPT 1
|
---|
| 860 | #define FS_TYPE_NEGOPT 2
|
---|
| 861 |
|
---|
| 862 | static const char fs_type_syntax_error[] =
|
---|
| 863 | "Either all or none of the filesystem types passed to -t must be prefixed\n"
|
---|
| 864 | "with 'no' or '!'.";
|
---|
| 865 |
|
---|
| 866 | static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
|
---|
| 867 | {
|
---|
| 868 | char *cp, *list, *s;
|
---|
| 869 | int num = 2;
|
---|
| 870 | int negate, first_negate = 1;
|
---|
| 871 |
|
---|
| 872 | if (fs_type) {
|
---|
| 873 | for (cp=fs_type; *cp; cp++) {
|
---|
| 874 | if (*cp == ',')
|
---|
| 875 | num++;
|
---|
| 876 | }
|
---|
| 877 | }
|
---|
| 878 |
|
---|
| 879 | cmp->list = xzalloc(num * sizeof(char *));
|
---|
| 880 | cmp->type = xzalloc(num * sizeof(int));
|
---|
| 881 | cmp->negate = 0;
|
---|
| 882 |
|
---|
| 883 | if (!fs_type)
|
---|
| 884 | return;
|
---|
| 885 |
|
---|
| 886 | list = string_copy(fs_type);
|
---|
| 887 | num = 0;
|
---|
| 888 | s = strtok(list, ",");
|
---|
| 889 | while(s) {
|
---|
| 890 | negate = 0;
|
---|
| 891 | if (strncmp(s, "no", 2) == 0) {
|
---|
| 892 | s += 2;
|
---|
| 893 | negate = 1;
|
---|
| 894 | } else if (*s == '!') {
|
---|
| 895 | s++;
|
---|
| 896 | negate = 1;
|
---|
| 897 | }
|
---|
| 898 | if (strcmp(s, "loop") == 0)
|
---|
| 899 | /* loop is really short-hand for opts=loop */
|
---|
| 900 | goto loop_special_case;
|
---|
| 901 | else if (strncmp(s, "opts=", 5) == 0) {
|
---|
| 902 | s += 5;
|
---|
| 903 | loop_special_case:
|
---|
| 904 | cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
|
---|
| 905 | } else {
|
---|
| 906 | if (first_negate) {
|
---|
| 907 | cmp->negate = negate;
|
---|
| 908 | first_negate = 0;
|
---|
| 909 | }
|
---|
| 910 | if ((negate && !cmp->negate) ||
|
---|
| 911 | (!negate && cmp->negate)) {
|
---|
| 912 | bb_error_msg_and_die("%s", fs_type_syntax_error);
|
---|
| 913 | }
|
---|
| 914 | }
|
---|
| 915 | cmp->list[num++] = string_copy(s);
|
---|
| 916 | s = strtok(NULL, ",");
|
---|
| 917 | }
|
---|
| 918 | free(list);
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | /*
|
---|
| 922 | * This function returns true if a particular option appears in a
|
---|
| 923 | * comma-delimited options list
|
---|
| 924 | */
|
---|
| 925 | static int opt_in_list(char *opt, char *optlist)
|
---|
| 926 | {
|
---|
| 927 | char *list, *s;
|
---|
| 928 |
|
---|
| 929 | if (!optlist)
|
---|
| 930 | return 0;
|
---|
| 931 | list = string_copy(optlist);
|
---|
| 932 |
|
---|
| 933 | s = strtok(list, ",");
|
---|
| 934 | while(s) {
|
---|
| 935 | if (strcmp(s, opt) == 0) {
|
---|
| 936 | free(list);
|
---|
| 937 | return 1;
|
---|
| 938 | }
|
---|
| 939 | s = strtok(NULL, ",");
|
---|
| 940 | }
|
---|
| 941 | free(list);
|
---|
| 942 | return 0;
|
---|
| 943 | }
|
---|
| 944 |
|
---|
| 945 | /* See if the filesystem matches the criteria given by the -t option */
|
---|
| 946 | static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
|
---|
| 947 | {
|
---|
| 948 | int n, ret = 0, checked_type = 0;
|
---|
| 949 | char *cp;
|
---|
| 950 |
|
---|
| 951 | if (cmp->list == 0 || cmp->list[0] == 0)
|
---|
| 952 | return 1;
|
---|
| 953 |
|
---|
| 954 | for (n=0; (cp = cmp->list[n]); n++) {
|
---|
| 955 | switch (cmp->type[n]) {
|
---|
| 956 | case FS_TYPE_NORMAL:
|
---|
| 957 | checked_type++;
|
---|
| 958 | if (strcmp(cp, fs->type) == 0) {
|
---|
| 959 | ret = 1;
|
---|
| 960 | }
|
---|
| 961 | break;
|
---|
| 962 | case FS_TYPE_NEGOPT:
|
---|
| 963 | if (opt_in_list(cp, fs->opts))
|
---|
| 964 | return 0;
|
---|
| 965 | break;
|
---|
| 966 | case FS_TYPE_OPT:
|
---|
| 967 | if (!opt_in_list(cp, fs->opts))
|
---|
| 968 | return 0;
|
---|
| 969 | break;
|
---|
| 970 | }
|
---|
| 971 | }
|
---|
| 972 | if (checked_type == 0)
|
---|
| 973 | return 1;
|
---|
| 974 | return (cmp->negate ? !ret : ret);
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | /* Check if we should ignore this filesystem. */
|
---|
| 978 | static int ignore(struct fs_info *fs)
|
---|
| 979 | {
|
---|
| 980 | int wanted;
|
---|
| 981 | char *s;
|
---|
| 982 |
|
---|
| 983 | /*
|
---|
| 984 | * If the pass number is 0, ignore it.
|
---|
| 985 | */
|
---|
| 986 | if (fs->passno == 0)
|
---|
| 987 | return 1;
|
---|
| 988 |
|
---|
| 989 | interpret_type(fs);
|
---|
| 990 |
|
---|
| 991 | /*
|
---|
| 992 | * If a specific fstype is specified, and it doesn't match,
|
---|
| 993 | * ignore it.
|
---|
| 994 | */
|
---|
| 995 | if (!fs_match(fs, &fs_type_compiled)) return 1;
|
---|
| 996 |
|
---|
| 997 | /* Are we ignoring this type? */
|
---|
| 998 | if (index_in_str_array(ignored_types, fs->type) >= 0)
|
---|
| 999 | return 1;
|
---|
| 1000 |
|
---|
| 1001 | /* Do we really really want to check this fs? */
|
---|
| 1002 | wanted = index_in_str_array(really_wanted, fs->type) >= 0;
|
---|
| 1003 |
|
---|
| 1004 | /* See if the <fsck.fs> program is available. */
|
---|
| 1005 | s = find_fsck(fs->type);
|
---|
| 1006 | if (s == NULL) {
|
---|
| 1007 | if (wanted)
|
---|
| 1008 | bb_error_msg("cannot check %s: fsck.%s not found",
|
---|
| 1009 | fs->device, fs->type);
|
---|
| 1010 | return 1;
|
---|
| 1011 | }
|
---|
| 1012 | free(s);
|
---|
| 1013 |
|
---|
| 1014 | /* We can and want to check this file system type. */
|
---|
| 1015 | return 0;
|
---|
| 1016 | }
|
---|
| 1017 |
|
---|
| 1018 | /*
|
---|
| 1019 | * Returns TRUE if a partition on the same disk is already being
|
---|
| 1020 | * checked.
|
---|
| 1021 | */
|
---|
| 1022 | static int device_already_active(char *device)
|
---|
| 1023 | {
|
---|
| 1024 | struct fsck_instance *inst;
|
---|
| 1025 | char *base;
|
---|
| 1026 |
|
---|
| 1027 | if (force_all_parallel)
|
---|
| 1028 | return 0;
|
---|
| 1029 |
|
---|
| 1030 | #ifdef BASE_MD
|
---|
| 1031 | /* Don't check a soft raid disk with any other disk */
|
---|
| 1032 | if (instance_list &&
|
---|
| 1033 | (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
|
---|
| 1034 | !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
|
---|
| 1035 | return 1;
|
---|
| 1036 | #endif
|
---|
| 1037 |
|
---|
| 1038 | base = base_device(device);
|
---|
| 1039 | /*
|
---|
| 1040 | * If we don't know the base device, assume that the device is
|
---|
| 1041 | * already active if there are any fsck instances running.
|
---|
| 1042 | */
|
---|
| 1043 | if (!base)
|
---|
| 1044 | return (instance_list != 0);
|
---|
| 1045 | for (inst = instance_list; inst; inst = inst->next) {
|
---|
| 1046 | if (!inst->base_device || !strcmp(base, inst->base_device)) {
|
---|
| 1047 | free(base);
|
---|
| 1048 | return 1;
|
---|
| 1049 | }
|
---|
| 1050 | }
|
---|
| 1051 | free(base);
|
---|
| 1052 | return 0;
|
---|
| 1053 | }
|
---|
| 1054 |
|
---|
| 1055 | /* Check all file systems, using the /etc/fstab table. */
|
---|
| 1056 | static int check_all(void)
|
---|
| 1057 | {
|
---|
| 1058 | struct fs_info *fs = NULL;
|
---|
| 1059 | int status = EXIT_OK;
|
---|
| 1060 | int not_done_yet = 1;
|
---|
| 1061 | int passno = 1;
|
---|
| 1062 | int pass_done;
|
---|
| 1063 |
|
---|
| 1064 | if (verbose)
|
---|
| 1065 | fputs("Checking all file systems.\n", stdout);
|
---|
| 1066 |
|
---|
| 1067 | /*
|
---|
| 1068 | * Do an initial scan over the filesystem; mark filesystems
|
---|
| 1069 | * which should be ignored as done, and resolve any "auto"
|
---|
| 1070 | * filesystem types (done as a side-effect of calling ignore()).
|
---|
| 1071 | */
|
---|
| 1072 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 1073 | if (ignore(fs))
|
---|
| 1074 | fs->flags |= FLAG_DONE;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | /*
|
---|
| 1078 | * Find and check the root filesystem.
|
---|
| 1079 | */
|
---|
| 1080 | if (!parallel_root) {
|
---|
| 1081 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 1082 | if (LONE_CHAR(fs->mountpt, '/'))
|
---|
| 1083 | break;
|
---|
| 1084 | }
|
---|
| 1085 | if (fs) {
|
---|
| 1086 | if (!skip_root && !ignore(fs)) {
|
---|
| 1087 | fsck_device(fs, 1);
|
---|
| 1088 | status |= wait_many(FLAG_WAIT_ALL);
|
---|
| 1089 | if (status > EXIT_NONDESTRUCT)
|
---|
| 1090 | return status;
|
---|
| 1091 | }
|
---|
| 1092 | fs->flags |= FLAG_DONE;
|
---|
| 1093 | }
|
---|
| 1094 | }
|
---|
| 1095 | /*
|
---|
| 1096 | * This is for the bone-headed user who enters the root
|
---|
| 1097 | * filesystem twice. Skip root will skep all root entries.
|
---|
| 1098 | */
|
---|
| 1099 | if (skip_root)
|
---|
| 1100 | for (fs = filesys_info; fs; fs = fs->next)
|
---|
| 1101 | if (LONE_CHAR(fs->mountpt, '/'))
|
---|
| 1102 | fs->flags |= FLAG_DONE;
|
---|
| 1103 |
|
---|
| 1104 | while (not_done_yet) {
|
---|
| 1105 | not_done_yet = 0;
|
---|
| 1106 | pass_done = 1;
|
---|
| 1107 |
|
---|
| 1108 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 1109 | if (cancel_requested)
|
---|
| 1110 | break;
|
---|
| 1111 | if (fs->flags & FLAG_DONE)
|
---|
| 1112 | continue;
|
---|
| 1113 | /*
|
---|
| 1114 | * If the filesystem's pass number is higher
|
---|
| 1115 | * than the current pass number, then we don't
|
---|
| 1116 | * do it yet.
|
---|
| 1117 | */
|
---|
| 1118 | if (fs->passno > passno) {
|
---|
| 1119 | not_done_yet++;
|
---|
| 1120 | continue;
|
---|
| 1121 | }
|
---|
| 1122 | /*
|
---|
| 1123 | * If a filesystem on a particular device has
|
---|
| 1124 | * already been spawned, then we need to defer
|
---|
| 1125 | * this to another pass.
|
---|
| 1126 | */
|
---|
| 1127 | if (device_already_active(fs->device)) {
|
---|
| 1128 | pass_done = 0;
|
---|
| 1129 | continue;
|
---|
| 1130 | }
|
---|
| 1131 | /*
|
---|
| 1132 | * Spawn off the fsck process
|
---|
| 1133 | */
|
---|
| 1134 | fsck_device(fs, serialize);
|
---|
| 1135 | fs->flags |= FLAG_DONE;
|
---|
| 1136 |
|
---|
| 1137 | /*
|
---|
| 1138 | * Only do one filesystem at a time, or if we
|
---|
| 1139 | * have a limit on the number of fsck's extant
|
---|
| 1140 | * at one time, apply that limit.
|
---|
| 1141 | */
|
---|
| 1142 | if (serialize ||
|
---|
| 1143 | (max_running && (num_running >= max_running))) {
|
---|
| 1144 | pass_done = 0;
|
---|
| 1145 | break;
|
---|
| 1146 | }
|
---|
| 1147 | }
|
---|
| 1148 | if (cancel_requested)
|
---|
| 1149 | break;
|
---|
| 1150 | if (verbose > 1)
|
---|
| 1151 | printf("--waiting-- (pass %d)\n", passno);
|
---|
| 1152 | status |= wait_many(pass_done ? FLAG_WAIT_ALL :
|
---|
| 1153 | FLAG_WAIT_ATLEAST_ONE);
|
---|
| 1154 | if (pass_done) {
|
---|
| 1155 | if (verbose > 1)
|
---|
| 1156 | printf("----------------------------------\n");
|
---|
| 1157 | passno++;
|
---|
| 1158 | } else
|
---|
| 1159 | not_done_yet++;
|
---|
| 1160 | }
|
---|
| 1161 | if (cancel_requested && !kill_sent) {
|
---|
| 1162 | kill_all(SIGTERM);
|
---|
| 1163 | kill_sent++;
|
---|
| 1164 | }
|
---|
| 1165 | status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
|
---|
| 1166 | return status;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | static void signal_cancel(int sig FSCK_ATTR((unused)))
|
---|
| 1170 | {
|
---|
| 1171 | cancel_requested++;
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
| 1174 | static void PRS(int argc, char **argv)
|
---|
| 1175 | {
|
---|
| 1176 | int i, j;
|
---|
| 1177 | char *arg, *dev, *tmp = 0;
|
---|
| 1178 | char options[128];
|
---|
| 1179 | int opt = 0;
|
---|
| 1180 | int opts_for_fsck = 0;
|
---|
| 1181 | struct sigaction sa;
|
---|
| 1182 |
|
---|
| 1183 | /*
|
---|
| 1184 | * Set up signal action
|
---|
| 1185 | */
|
---|
| 1186 | memset(&sa, 0, sizeof(struct sigaction));
|
---|
| 1187 | sa.sa_handler = signal_cancel;
|
---|
| 1188 | sigaction(SIGINT, &sa, 0);
|
---|
| 1189 | sigaction(SIGTERM, &sa, 0);
|
---|
| 1190 |
|
---|
| 1191 | num_devices = 0;
|
---|
| 1192 | num_args = 0;
|
---|
| 1193 | instance_list = 0;
|
---|
| 1194 |
|
---|
| 1195 | for (i=1; i < argc; i++) {
|
---|
| 1196 | arg = argv[i];
|
---|
| 1197 | if (!arg)
|
---|
| 1198 | continue;
|
---|
| 1199 | if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
|
---|
| 1200 | if (num_devices >= MAX_DEVICES) {
|
---|
| 1201 | bb_error_msg_and_die("too many devices");
|
---|
| 1202 | }
|
---|
| 1203 | dev = blkid_get_devname(cache, arg, NULL);
|
---|
| 1204 | if (!dev && strchr(arg, '=')) {
|
---|
| 1205 | /*
|
---|
| 1206 | * Check to see if we failed because
|
---|
| 1207 | * /proc/partitions isn't found.
|
---|
| 1208 | */
|
---|
| 1209 | if (access("/proc/partitions", R_OK) < 0) {
|
---|
| 1210 | bb_perror_msg_and_die("cannot open /proc/partitions "
|
---|
| 1211 | "(is /proc mounted?)");
|
---|
| 1212 | }
|
---|
| 1213 | /*
|
---|
| 1214 | * Check to see if this is because
|
---|
| 1215 | * we're not running as root
|
---|
| 1216 | */
|
---|
| 1217 | if (geteuid())
|
---|
| 1218 | bb_error_msg_and_die(
|
---|
| 1219 | "must be root to scan for matching filesystems: %s\n", arg);
|
---|
| 1220 | else
|
---|
| 1221 | bb_error_msg_and_die(
|
---|
| 1222 | "cannot find matching filesystem: %s", arg);
|
---|
| 1223 | }
|
---|
| 1224 | devices[num_devices++] = dev ? dev : string_copy(arg);
|
---|
| 1225 | continue;
|
---|
| 1226 | }
|
---|
| 1227 | if (arg[0] != '-' || opts_for_fsck) {
|
---|
| 1228 | if (num_args >= MAX_ARGS) {
|
---|
| 1229 | bb_error_msg_and_die("too many arguments");
|
---|
| 1230 | }
|
---|
| 1231 | args[num_args++] = string_copy(arg);
|
---|
| 1232 | continue;
|
---|
| 1233 | }
|
---|
| 1234 | for (j=1; arg[j]; j++) {
|
---|
| 1235 | if (opts_for_fsck) {
|
---|
| 1236 | options[++opt] = arg[j];
|
---|
| 1237 | continue;
|
---|
| 1238 | }
|
---|
| 1239 | switch (arg[j]) {
|
---|
| 1240 | case 'A':
|
---|
| 1241 | doall++;
|
---|
| 1242 | break;
|
---|
| 1243 | case 'C':
|
---|
| 1244 | progress++;
|
---|
| 1245 | if (arg[j+1]) {
|
---|
| 1246 | progress_fd = string_to_int(arg+j+1);
|
---|
| 1247 | if (progress_fd < 0)
|
---|
| 1248 | progress_fd = 0;
|
---|
| 1249 | else
|
---|
| 1250 | goto next_arg;
|
---|
| 1251 | } else if ((i+1) < argc
|
---|
| 1252 | && argv[i+1][0] != '-') {
|
---|
| 1253 | progress_fd = string_to_int(argv[i]);
|
---|
| 1254 | if (progress_fd < 0)
|
---|
| 1255 | progress_fd = 0;
|
---|
| 1256 | else {
|
---|
| 1257 | goto next_arg;
|
---|
| 1258 | i++;
|
---|
| 1259 | }
|
---|
| 1260 | }
|
---|
| 1261 | break;
|
---|
| 1262 | case 'V':
|
---|
| 1263 | verbose++;
|
---|
| 1264 | break;
|
---|
| 1265 | case 'N':
|
---|
| 1266 | noexecute++;
|
---|
| 1267 | break;
|
---|
| 1268 | case 'R':
|
---|
| 1269 | skip_root++;
|
---|
| 1270 | break;
|
---|
| 1271 | case 'T':
|
---|
| 1272 | notitle++;
|
---|
| 1273 | break;
|
---|
| 1274 | case 'M':
|
---|
| 1275 | like_mount++;
|
---|
| 1276 | break;
|
---|
| 1277 | case 'P':
|
---|
| 1278 | parallel_root++;
|
---|
| 1279 | break;
|
---|
| 1280 | case 's':
|
---|
| 1281 | serialize++;
|
---|
| 1282 | break;
|
---|
| 1283 | case 't':
|
---|
| 1284 | tmp = 0;
|
---|
| 1285 | if (fstype)
|
---|
| 1286 | bb_show_usage();
|
---|
| 1287 | if (arg[j+1])
|
---|
| 1288 | tmp = arg+j+1;
|
---|
| 1289 | else if ((i+1) < argc)
|
---|
| 1290 | tmp = argv[++i];
|
---|
| 1291 | else
|
---|
| 1292 | bb_show_usage();
|
---|
| 1293 | fstype = string_copy(tmp);
|
---|
| 1294 | compile_fs_type(fstype, &fs_type_compiled);
|
---|
| 1295 | goto next_arg;
|
---|
| 1296 | case '-':
|
---|
| 1297 | opts_for_fsck++;
|
---|
| 1298 | break;
|
---|
| 1299 | case '?':
|
---|
| 1300 | bb_show_usage();
|
---|
| 1301 | break;
|
---|
| 1302 | default:
|
---|
| 1303 | options[++opt] = arg[j];
|
---|
| 1304 | break;
|
---|
| 1305 | }
|
---|
| 1306 | }
|
---|
| 1307 | next_arg:
|
---|
| 1308 | if (opt) {
|
---|
| 1309 | options[0] = '-';
|
---|
| 1310 | options[++opt] = '\0';
|
---|
| 1311 | if (num_args >= MAX_ARGS) {
|
---|
| 1312 | bb_error_msg("too many arguments");
|
---|
| 1313 | }
|
---|
| 1314 | args[num_args++] = string_copy(options);
|
---|
| 1315 | opt = 0;
|
---|
| 1316 | }
|
---|
| 1317 | }
|
---|
| 1318 | if (getenv("FSCK_FORCE_ALL_PARALLEL"))
|
---|
| 1319 | force_all_parallel++;
|
---|
| 1320 | if ((tmp = getenv("FSCK_MAX_INST")))
|
---|
| 1321 | max_running = atoi(tmp);
|
---|
| 1322 | }
|
---|
| 1323 |
|
---|
| 1324 | int fsck_main(int argc, char **argv);
|
---|
| 1325 | int fsck_main(int argc, char **argv)
|
---|
| 1326 | {
|
---|
| 1327 | int i, status = 0;
|
---|
| 1328 | int interactive = 0;
|
---|
| 1329 | const char *fstab;
|
---|
| 1330 | struct fs_info *fs;
|
---|
| 1331 |
|
---|
| 1332 | setvbuf(stdout, NULL, _IONBF, BUFSIZ);
|
---|
| 1333 | setvbuf(stderr, NULL, _IONBF, BUFSIZ);
|
---|
| 1334 |
|
---|
| 1335 | blkid_get_cache(&cache, NULL);
|
---|
| 1336 | PRS(argc, argv);
|
---|
| 1337 |
|
---|
| 1338 | if (!notitle)
|
---|
| 1339 | printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
|
---|
| 1340 |
|
---|
| 1341 | fstab = getenv("FSTAB_FILE");
|
---|
| 1342 | if (!fstab)
|
---|
| 1343 | fstab = _PATH_MNTTAB;
|
---|
| 1344 | load_fs_info(fstab);
|
---|
| 1345 |
|
---|
| 1346 | fsck_path = e2fs_set_sbin_path();
|
---|
| 1347 |
|
---|
| 1348 | if ((num_devices == 1) || (serialize))
|
---|
| 1349 | interactive = 1;
|
---|
| 1350 |
|
---|
| 1351 | /* If -A was specified ("check all"), do that! */
|
---|
| 1352 | if (doall)
|
---|
| 1353 | return check_all();
|
---|
| 1354 |
|
---|
| 1355 | if (num_devices == 0) {
|
---|
| 1356 | serialize++;
|
---|
| 1357 | interactive++;
|
---|
| 1358 | return check_all();
|
---|
| 1359 | }
|
---|
| 1360 | for (i = 0; i < num_devices; i++) {
|
---|
| 1361 | if (cancel_requested) {
|
---|
| 1362 | if (!kill_sent) {
|
---|
| 1363 | kill_all(SIGTERM);
|
---|
| 1364 | kill_sent++;
|
---|
| 1365 | }
|
---|
| 1366 | break;
|
---|
| 1367 | }
|
---|
| 1368 | fs = lookup(devices[i]);
|
---|
| 1369 | if (!fs) {
|
---|
| 1370 | fs = create_fs_device(devices[i], 0, "auto",
|
---|
| 1371 | 0, -1, -1);
|
---|
| 1372 | if (!fs)
|
---|
| 1373 | continue;
|
---|
| 1374 | }
|
---|
| 1375 | fsck_device(fs, interactive);
|
---|
| 1376 | if (serialize ||
|
---|
| 1377 | (max_running && (num_running >= max_running))) {
|
---|
| 1378 | struct fsck_instance *inst;
|
---|
| 1379 |
|
---|
| 1380 | inst = wait_one(0);
|
---|
| 1381 | if (inst) {
|
---|
| 1382 | status |= inst->exit_status;
|
---|
| 1383 | free_instance(inst);
|
---|
| 1384 | }
|
---|
| 1385 | if (verbose > 1)
|
---|
| 1386 | printf("----------------------------------\n");
|
---|
| 1387 | }
|
---|
| 1388 | }
|
---|
| 1389 | status |= wait_many(FLAG_WAIT_ALL);
|
---|
| 1390 | blkid_put_cache(cache);
|
---|
| 1391 | return status;
|
---|
| 1392 | }
|
---|