[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
[1765] | 3 | * fsck --- A generic, parallelizing front-end for the fsck program.
|
---|
[821] | 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 |
|
---|
[1765] | 29 | /* All filesystem specific hooks have been removed.
|
---|
| 30 | * If filesystem cannot be determined, we will execute
|
---|
| 31 | * "fsck.auto". Currently this also happens if you specify
|
---|
| 32 | * UUID=xxx or LABEL=xxx as an object to check.
|
---|
| 33 | * Detection code for that is also probably has to be in fsck.auto.
|
---|
| 34 | *
|
---|
| 35 | * In other words, this is _really_ is just a driver program which
|
---|
| 36 | * spawns actual fsck.something for each filesystem to check.
|
---|
| 37 | * It doesn't guess filesystem types from on-disk format.
|
---|
[821] | 38 | */
|
---|
| 39 |
|
---|
[1765] | 40 | #include "libbb.h"
|
---|
[821] | 41 |
|
---|
[1765] | 42 | #define EXIT_OK 0
|
---|
| 43 | #define EXIT_NONDESTRUCT 1
|
---|
| 44 | #define EXIT_DESTRUCT 2
|
---|
| 45 | #define EXIT_UNCORRECTED 4
|
---|
| 46 | #define EXIT_ERROR 8
|
---|
| 47 | #define EXIT_USAGE 16
|
---|
| 48 | #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
|
---|
[821] | 49 |
|
---|
| 50 | /*
|
---|
[1765] | 51 | * Internal structure for mount table entries.
|
---|
[821] | 52 | */
|
---|
| 53 |
|
---|
| 54 | struct fs_info {
|
---|
| 55 | struct fs_info *next;
|
---|
[1765] | 56 | char *device;
|
---|
| 57 | char *mountpt;
|
---|
| 58 | char *type;
|
---|
| 59 | char *opts;
|
---|
| 60 | int freq;
|
---|
| 61 | int passno;
|
---|
| 62 | int flags;
|
---|
[821] | 63 | };
|
---|
| 64 |
|
---|
| 65 | #define FLAG_DONE 1
|
---|
| 66 | #define FLAG_PROGRESS 2
|
---|
| 67 | /*
|
---|
| 68 | * Structure to allow exit codes to be stored
|
---|
| 69 | */
|
---|
| 70 | struct fsck_instance {
|
---|
[1765] | 71 | struct fsck_instance *next;
|
---|
[821] | 72 | int pid;
|
---|
| 73 | int flags;
|
---|
| 74 | int exit_status;
|
---|
| 75 | time_t start_time;
|
---|
[1765] | 76 | char *prog;
|
---|
| 77 | char *type;
|
---|
| 78 | char *device;
|
---|
| 79 | char *base_device; /* /dev/hda for /dev/hdaN etc */
|
---|
[821] | 80 | };
|
---|
| 81 |
|
---|
[1765] | 82 | static const char ignored_types[] ALIGN1 =
|
---|
| 83 | "ignore\0"
|
---|
| 84 | "iso9660\0"
|
---|
| 85 | "nfs\0"
|
---|
| 86 | "proc\0"
|
---|
| 87 | "sw\0"
|
---|
| 88 | "swap\0"
|
---|
| 89 | "tmpfs\0"
|
---|
| 90 | "devpts\0";
|
---|
| 91 |
|
---|
| 92 | #if 0
|
---|
| 93 | static const char really_wanted[] ALIGN1 =
|
---|
| 94 | "minix\0"
|
---|
| 95 | "ext2\0"
|
---|
| 96 | "ext3\0"
|
---|
| 97 | "jfs\0"
|
---|
| 98 | "reiserfs\0"
|
---|
| 99 | "xiafs\0"
|
---|
| 100 | "xfs\0";
|
---|
| 101 | #endif
|
---|
| 102 |
|
---|
| 103 | #define BASE_MD "/dev/md"
|
---|
| 104 |
|
---|
| 105 | static char **devices;
|
---|
| 106 | static char **args;
|
---|
| 107 | static int num_devices;
|
---|
| 108 | static int num_args;
|
---|
| 109 | static int verbose;
|
---|
| 110 |
|
---|
| 111 | #define FS_TYPE_FLAG_NORMAL 0
|
---|
| 112 | #define FS_TYPE_FLAG_OPT 1
|
---|
| 113 | #define FS_TYPE_FLAG_NEGOPT 2
|
---|
| 114 | static char **fs_type_list;
|
---|
| 115 | static uint8_t *fs_type_flag;
|
---|
| 116 | static smallint fs_type_negated;
|
---|
| 117 |
|
---|
| 118 | static volatile smallint cancel_requested;
|
---|
| 119 | static smallint doall;
|
---|
| 120 | static smallint noexecute;
|
---|
| 121 | static smallint serialize;
|
---|
| 122 | static smallint skip_root;
|
---|
| 123 | /* static smallint like_mount; */
|
---|
| 124 | static smallint notitle;
|
---|
| 125 | static smallint parallel_root;
|
---|
| 126 | static smallint force_all_parallel;
|
---|
| 127 |
|
---|
| 128 | /* "progress indicator" code is somewhat buggy and ext[23] specific.
|
---|
| 129 | * We should be filesystem agnostic. IOW: there should be a well-defined
|
---|
| 130 | * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
|
---|
| 131 | #define DO_PROGRESS_INDICATOR 0
|
---|
| 132 | #if DO_PROGRESS_INDICATOR
|
---|
| 133 | static smallint progress;
|
---|
| 134 | static int progress_fd;
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 | static int num_running;
|
---|
| 138 | static int max_running;
|
---|
| 139 | static char *fstype;
|
---|
| 140 | static struct fs_info *filesys_info;
|
---|
| 141 | static struct fs_info *filesys_last;
|
---|
| 142 | static struct fsck_instance *instance_list;
|
---|
| 143 |
|
---|
[821] | 144 | /*
|
---|
| 145 | * Return the "base device" given a particular device; this is used to
|
---|
| 146 | * assure that we only fsck one partition on a particular drive at any
|
---|
| 147 | * one time. Otherwise, the disk heads will be seeking all over the
|
---|
[1765] | 148 | * place. If the base device cannot be determined, return NULL.
|
---|
[821] | 149 | *
|
---|
| 150 | * The base_device() function returns an allocated string which must
|
---|
| 151 | * be freed.
|
---|
| 152 | */
|
---|
[1765] | 153 | #if ENABLE_FEATURE_DEVFS
|
---|
[821] | 154 | /*
|
---|
| 155 | * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
|
---|
| 156 | * pathames.
|
---|
| 157 | */
|
---|
[1765] | 158 | static const char *const devfs_hier[] = {
|
---|
| 159 | "host", "bus", "target", "lun", NULL
|
---|
[821] | 160 | };
|
---|
| 161 | #endif
|
---|
| 162 |
|
---|
| 163 | static char *base_device(const char *device)
|
---|
| 164 | {
|
---|
| 165 | char *str, *cp;
|
---|
[1765] | 166 | #if ENABLE_FEATURE_DEVFS
|
---|
| 167 | const char *const *hier;
|
---|
[821] | 168 | const char *disk;
|
---|
| 169 | int len;
|
---|
| 170 | #endif
|
---|
[1765] | 171 | cp = str = xstrdup(device);
|
---|
[821] | 172 |
|
---|
| 173 | /* Skip over /dev/; if it's not present, give up. */
|
---|
| 174 | if (strncmp(cp, "/dev/", 5) != 0)
|
---|
| 175 | goto errout;
|
---|
| 176 | cp += 5;
|
---|
| 177 |
|
---|
| 178 | /*
|
---|
| 179 | * For md devices, we treat them all as if they were all
|
---|
| 180 | * on one disk, since we don't know how to parallelize them.
|
---|
| 181 | */
|
---|
| 182 | if (cp[0] == 'm' && cp[1] == 'd') {
|
---|
[1765] | 183 | cp[2] = 0;
|
---|
[821] | 184 | return str;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /* Handle DAC 960 devices */
|
---|
| 188 | if (strncmp(cp, "rd/", 3) == 0) {
|
---|
| 189 | cp += 3;
|
---|
[1765] | 190 | if (cp[0] != 'c' || !isdigit(cp[1])
|
---|
| 191 | || cp[2] != 'd' || !isdigit(cp[3]))
|
---|
[821] | 192 | goto errout;
|
---|
[1765] | 193 | cp[4] = 0;
|
---|
[821] | 194 | return str;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /* Now let's handle /dev/hd* and /dev/sd* devices.... */
|
---|
[1765] | 198 | if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
|
---|
[821] | 199 | cp += 2;
|
---|
| 200 | /* If there's a single number after /dev/hd, skip it */
|
---|
| 201 | if (isdigit(*cp))
|
---|
| 202 | cp++;
|
---|
| 203 | /* What follows must be an alpha char, or give up */
|
---|
| 204 | if (!isalpha(*cp))
|
---|
| 205 | goto errout;
|
---|
[1765] | 206 | cp[1] = 0;
|
---|
[821] | 207 | return str;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[1765] | 210 | #if ENABLE_FEATURE_DEVFS
|
---|
[821] | 211 | /* Now let's handle devfs (ugh) names */
|
---|
| 212 | len = 0;
|
---|
| 213 | if (strncmp(cp, "ide/", 4) == 0)
|
---|
| 214 | len = 4;
|
---|
| 215 | if (strncmp(cp, "scsi/", 5) == 0)
|
---|
| 216 | len = 5;
|
---|
| 217 | if (len) {
|
---|
| 218 | cp += len;
|
---|
| 219 | /*
|
---|
| 220 | * Now we proceed down the expected devfs hierarchy.
|
---|
| 221 | * i.e., .../host1/bus2/target3/lun4/...
|
---|
| 222 | * If we don't find the expected token, followed by
|
---|
| 223 | * some number of digits at each level, abort.
|
---|
| 224 | */
|
---|
| 225 | for (hier = devfs_hier; *hier; hier++) {
|
---|
| 226 | len = strlen(*hier);
|
---|
| 227 | if (strncmp(cp, *hier, len) != 0)
|
---|
| 228 | goto errout;
|
---|
| 229 | cp += len;
|
---|
| 230 | while (*cp != '/' && *cp != 0) {
|
---|
| 231 | if (!isdigit(*cp))
|
---|
| 232 | goto errout;
|
---|
| 233 | cp++;
|
---|
| 234 | }
|
---|
| 235 | cp++;
|
---|
| 236 | }
|
---|
[1765] | 237 | cp[-1] = 0;
|
---|
[821] | 238 | return str;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /* Now handle devfs /dev/disc or /dev/disk names */
|
---|
| 242 | disk = 0;
|
---|
| 243 | if (strncmp(cp, "discs/", 6) == 0)
|
---|
| 244 | disk = "disc";
|
---|
| 245 | else if (strncmp(cp, "disks/", 6) == 0)
|
---|
| 246 | disk = "disk";
|
---|
| 247 | if (disk) {
|
---|
| 248 | cp += 6;
|
---|
| 249 | if (strncmp(cp, disk, 4) != 0)
|
---|
| 250 | goto errout;
|
---|
| 251 | cp += 4;
|
---|
| 252 | while (*cp != '/' && *cp != 0) {
|
---|
| 253 | if (!isdigit(*cp))
|
---|
| 254 | goto errout;
|
---|
| 255 | cp++;
|
---|
| 256 | }
|
---|
| 257 | *cp = 0;
|
---|
| 258 | return str;
|
---|
| 259 | }
|
---|
| 260 | #endif
|
---|
[1765] | 261 | errout:
|
---|
[821] | 262 | free(str);
|
---|
| 263 | return NULL;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[1765] | 266 | static void free_instance(struct fsck_instance *p)
|
---|
[821] | 267 | {
|
---|
[1765] | 268 | free(p->prog);
|
---|
| 269 | free(p->device);
|
---|
| 270 | free(p->base_device);
|
---|
| 271 | free(p);
|
---|
[821] | 272 | }
|
---|
| 273 |
|
---|
[1765] | 274 | static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
|
---|
| 275 | const char *type, const char *opts,
|
---|
| 276 | int freq, int passno)
|
---|
[821] | 277 | {
|
---|
[1765] | 278 | struct fs_info *fs;
|
---|
[821] | 279 |
|
---|
[1765] | 280 | fs = xzalloc(sizeof(*fs));
|
---|
| 281 | fs->device = xstrdup(device);
|
---|
| 282 | fs->mountpt = xstrdup(mntpnt);
|
---|
| 283 | fs->type = xstrdup(type);
|
---|
| 284 | fs->opts = xstrdup(opts ? opts : "");
|
---|
| 285 | fs->freq = freq;
|
---|
| 286 | fs->passno = passno;
|
---|
| 287 | /*fs->flags = 0; */
|
---|
| 288 | /*fs->next = NULL; */
|
---|
| 289 |
|
---|
| 290 | if (!filesys_info)
|
---|
| 291 | filesys_info = fs;
|
---|
[821] | 292 | else
|
---|
[1765] | 293 | filesys_last->next = fs;
|
---|
| 294 | filesys_last = fs;
|
---|
[821] | 295 |
|
---|
[1765] | 296 | return fs;
|
---|
[821] | 297 | }
|
---|
| 298 |
|
---|
| 299 | static void strip_line(char *line)
|
---|
| 300 | {
|
---|
[1765] | 301 | char *p = line + strlen(line) - 1;
|
---|
[821] | 302 |
|
---|
| 303 | while (*line) {
|
---|
[1765] | 304 | if (*p != '\n' && *p != '\r')
|
---|
[821] | 305 | break;
|
---|
[1765] | 306 | *p-- = '\0';
|
---|
[821] | 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | static char *parse_word(char **buf)
|
---|
| 311 | {
|
---|
| 312 | char *word, *next;
|
---|
| 313 |
|
---|
| 314 | word = *buf;
|
---|
[1765] | 315 | if (*word == '\0')
|
---|
| 316 | return NULL;
|
---|
[821] | 317 |
|
---|
[1765] | 318 | word = skip_whitespace(word);
|
---|
| 319 | next = skip_non_whitespace(word);
|
---|
[821] | 320 | if (*next)
|
---|
[1765] | 321 | *next++ = '\0';
|
---|
[821] | 322 | *buf = next;
|
---|
| 323 | return word;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | static void parse_escape(char *word)
|
---|
| 327 | {
|
---|
[1765] | 328 | char *q, c;
|
---|
[821] | 329 | const char *p;
|
---|
| 330 |
|
---|
| 331 | if (!word)
|
---|
| 332 | return;
|
---|
| 333 |
|
---|
| 334 | for (p = q = word; *p; q++) {
|
---|
| 335 | c = *p++;
|
---|
| 336 | if (c != '\\') {
|
---|
| 337 | *q = c;
|
---|
| 338 | } else {
|
---|
| 339 | *q = bb_process_escape_sequence(&p);
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
[1765] | 342 | *q = '\0';
|
---|
[821] | 343 | }
|
---|
| 344 |
|
---|
| 345 | static int parse_fstab_line(char *line, struct fs_info **ret_fs)
|
---|
| 346 | {
|
---|
[1765] | 347 | char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
|
---|
[821] | 348 | struct fs_info *fs;
|
---|
| 349 |
|
---|
| 350 | *ret_fs = 0;
|
---|
| 351 | strip_line(line);
|
---|
[1765] | 352 | cp = strchr(line, '#');
|
---|
| 353 | if (cp)
|
---|
| 354 | *cp = '\0'; /* Ignore everything after the comment char */
|
---|
[821] | 355 | cp = line;
|
---|
| 356 |
|
---|
| 357 | device = parse_word(&cp);
|
---|
[1765] | 358 | if (!device) return 0; /* Allow blank lines */
|
---|
[821] | 359 | mntpnt = parse_word(&cp);
|
---|
| 360 | type = parse_word(&cp);
|
---|
| 361 | opts = parse_word(&cp);
|
---|
| 362 | freq = parse_word(&cp);
|
---|
| 363 | passno = parse_word(&cp);
|
---|
| 364 |
|
---|
| 365 | if (!mntpnt || !type)
|
---|
| 366 | return -1;
|
---|
| 367 |
|
---|
| 368 | parse_escape(device);
|
---|
| 369 | parse_escape(mntpnt);
|
---|
| 370 | parse_escape(type);
|
---|
| 371 | parse_escape(opts);
|
---|
| 372 | parse_escape(freq);
|
---|
| 373 | parse_escape(passno);
|
---|
| 374 |
|
---|
| 375 | if (strchr(type, ','))
|
---|
[1765] | 376 | type = NULL;
|
---|
[821] | 377 |
|
---|
| 378 | fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
|
---|
[1765] | 379 | freq ? atoi(freq) : -1,
|
---|
| 380 | passno ? atoi(passno) : -1);
|
---|
[821] | 381 | *ret_fs = fs;
|
---|
| 382 | return 0;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
[1765] | 385 | /* Load the filesystem database from /etc/fstab */
|
---|
[821] | 386 | static void load_fs_info(const char *filename)
|
---|
| 387 | {
|
---|
[1765] | 388 | FILE *f;
|
---|
| 389 | int lineno = 0;
|
---|
| 390 | int old_fstab = 1;
|
---|
[821] | 391 | struct fs_info *fs;
|
---|
| 392 |
|
---|
[1765] | 393 | f = fopen_or_warn(filename, "r");
|
---|
| 394 | if (f == NULL) {
|
---|
[821] | 395 | return;
|
---|
| 396 | }
|
---|
[1765] | 397 | while (1) {
|
---|
| 398 | int r;
|
---|
| 399 | char *buf = xmalloc_getline(f);
|
---|
| 400 | if (!buf) break;
|
---|
| 401 | r = parse_fstab_line(buf, &fs);
|
---|
| 402 | free(buf);
|
---|
[821] | 403 | lineno++;
|
---|
[1765] | 404 | if (r < 0) {
|
---|
[821] | 405 | bb_error_msg("WARNING: bad format "
|
---|
[1765] | 406 | "on line %d of %s", lineno, filename);
|
---|
[821] | 407 | continue;
|
---|
| 408 | }
|
---|
| 409 | if (!fs)
|
---|
| 410 | continue;
|
---|
| 411 | if (fs->passno < 0)
|
---|
| 412 | fs->passno = 0;
|
---|
| 413 | else
|
---|
| 414 | old_fstab = 0;
|
---|
| 415 | }
|
---|
| 416 | fclose(f);
|
---|
| 417 |
|
---|
| 418 | if (old_fstab) {
|
---|
[1765] | 419 | fputs("\007"
|
---|
| 420 | "WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
|
---|
| 421 | "I will kludge around things for you, but you should fix\n"
|
---|
| 422 | "your /etc/fstab file as soon as you can.\n\n", stderr);
|
---|
[821] | 423 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 424 | fs->passno = 1;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | /* Lookup filesys in /etc/fstab and return the corresponding entry. */
|
---|
| 430 | static struct fs_info *lookup(char *filesys)
|
---|
| 431 | {
|
---|
| 432 | struct fs_info *fs;
|
---|
| 433 |
|
---|
| 434 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
[1765] | 435 | if (strcmp(filesys, fs->device) == 0
|
---|
| 436 | || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
|
---|
| 437 | )
|
---|
[821] | 438 | break;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | return fs;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[1765] | 444 | #if DO_PROGRESS_INDICATOR
|
---|
[821] | 445 | static int progress_active(void)
|
---|
| 446 | {
|
---|
| 447 | struct fsck_instance *inst;
|
---|
| 448 |
|
---|
| 449 | for (inst = instance_list; inst; inst = inst->next) {
|
---|
| 450 | if (inst->flags & FLAG_DONE)
|
---|
| 451 | continue;
|
---|
| 452 | if (inst->flags & FLAG_PROGRESS)
|
---|
| 453 | return 1;
|
---|
| 454 | }
|
---|
| 455 | return 0;
|
---|
| 456 | }
|
---|
[1765] | 457 | #endif
|
---|
[821] | 458 |
|
---|
| 459 |
|
---|
| 460 | /*
|
---|
| 461 | * Send a signal to all outstanding fsck child processes
|
---|
| 462 | */
|
---|
[1765] | 463 | static void kill_all_if_cancel_requested(void)
|
---|
[821] | 464 | {
|
---|
[1765] | 465 | static smallint kill_sent;
|
---|
| 466 |
|
---|
[821] | 467 | struct fsck_instance *inst;
|
---|
| 468 |
|
---|
[1765] | 469 | if (!cancel_requested || kill_sent)
|
---|
| 470 | return;
|
---|
| 471 |
|
---|
[821] | 472 | for (inst = instance_list; inst; inst = inst->next) {
|
---|
| 473 | if (inst->flags & FLAG_DONE)
|
---|
| 474 | continue;
|
---|
[1765] | 475 | kill(inst->pid, SIGTERM);
|
---|
[821] | 476 | }
|
---|
[1765] | 477 | kill_sent = 1;
|
---|
[821] | 478 | }
|
---|
| 479 |
|
---|
| 480 | /*
|
---|
| 481 | * Wait for one child process to exit; when it does, unlink it from
|
---|
| 482 | * the list of executing child processes, and return it.
|
---|
| 483 | */
|
---|
| 484 | static struct fsck_instance *wait_one(int flags)
|
---|
| 485 | {
|
---|
[1765] | 486 | int status;
|
---|
| 487 | int sig;
|
---|
| 488 | struct fsck_instance *inst, *prev;
|
---|
| 489 | pid_t pid;
|
---|
[821] | 490 |
|
---|
| 491 | if (!instance_list)
|
---|
| 492 | return NULL;
|
---|
| 493 |
|
---|
| 494 | if (noexecute) {
|
---|
| 495 | inst = instance_list;
|
---|
[1765] | 496 | prev = NULL;
|
---|
[821] | 497 | #ifdef RANDOM_DEBUG
|
---|
| 498 | while (inst->next && (random() & 1)) {
|
---|
| 499 | prev = inst;
|
---|
| 500 | inst = inst->next;
|
---|
| 501 | }
|
---|
| 502 | #endif
|
---|
| 503 | inst->exit_status = 0;
|
---|
| 504 | goto ret_inst;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[1765] | 507 | inst = prev = NULL; /* for gcc */
|
---|
[821] | 508 | do {
|
---|
| 509 | pid = waitpid(-1, &status, flags);
|
---|
[1765] | 510 | kill_all_if_cancel_requested();
|
---|
| 511 | if (pid == 0 && (flags & WNOHANG))
|
---|
[821] | 512 | return NULL;
|
---|
| 513 | if (pid < 0) {
|
---|
[1765] | 514 | if (errno == EINTR || errno == EAGAIN)
|
---|
[821] | 515 | continue;
|
---|
| 516 | if (errno == ECHILD) {
|
---|
[1765] | 517 | bb_error_msg("wait: no more child process?!?");
|
---|
[821] | 518 | return NULL;
|
---|
| 519 | }
|
---|
[1765] | 520 | bb_perror_msg("wait");
|
---|
[821] | 521 | continue;
|
---|
| 522 | }
|
---|
[1765] | 523 | prev = NULL;
|
---|
| 524 | inst = instance_list;
|
---|
| 525 | while (inst) {
|
---|
[821] | 526 | if (inst->pid == pid)
|
---|
| 527 | break;
|
---|
[1765] | 528 | prev = inst;
|
---|
| 529 | inst = inst->next;
|
---|
[821] | 530 | }
|
---|
| 531 | } while (!inst);
|
---|
| 532 |
|
---|
| 533 | if (WIFEXITED(status))
|
---|
| 534 | status = WEXITSTATUS(status);
|
---|
| 535 | else if (WIFSIGNALED(status)) {
|
---|
| 536 | sig = WTERMSIG(status);
|
---|
[1765] | 537 | status = EXIT_UNCORRECTED;
|
---|
| 538 | if (sig != SIGINT) {
|
---|
| 539 | printf("Warning... %s %s exited "
|
---|
| 540 | "with signal %d\n",
|
---|
| 541 | inst->prog, inst->device, sig);
|
---|
[821] | 542 | status = EXIT_ERROR;
|
---|
| 543 | }
|
---|
| 544 | } else {
|
---|
[1765] | 545 | printf("%s %s: status is %x, should never happen\n",
|
---|
| 546 | inst->prog, inst->device, status);
|
---|
[821] | 547 | status = EXIT_ERROR;
|
---|
| 548 | }
|
---|
| 549 | inst->exit_status = status;
|
---|
[1765] | 550 |
|
---|
| 551 | #if DO_PROGRESS_INDICATOR
|
---|
| 552 | if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
|
---|
| 553 | struct fsck_instance *inst2;
|
---|
[821] | 554 | for (inst2 = instance_list; inst2; inst2 = inst2->next) {
|
---|
| 555 | if (inst2->flags & FLAG_DONE)
|
---|
| 556 | continue;
|
---|
[1765] | 557 | if (strcmp(inst2->type, "ext2") != 0
|
---|
| 558 | && strcmp(inst2->type, "ext3") != 0
|
---|
| 559 | ) {
|
---|
[821] | 560 | continue;
|
---|
[1765] | 561 | }
|
---|
| 562 | /* ext[23], we will send USR1
|
---|
| 563 | * (request to start displaying progress bar)
|
---|
| 564 | *
|
---|
[821] | 565 | * If we've just started the fsck, wait a tiny
|
---|
| 566 | * bit before sending the kill, to give it
|
---|
| 567 | * time to set up the signal handler
|
---|
| 568 | */
|
---|
[1765] | 569 | if (inst2->start_time >= time(NULL) - 1)
|
---|
| 570 | sleep(1);
|
---|
| 571 | kill(inst2->pid, SIGUSR1);
|
---|
[821] | 572 | inst2->flags |= FLAG_PROGRESS;
|
---|
| 573 | break;
|
---|
| 574 | }
|
---|
| 575 | }
|
---|
[1765] | 576 | #endif
|
---|
| 577 |
|
---|
| 578 | ret_inst:
|
---|
[821] | 579 | if (prev)
|
---|
| 580 | prev->next = inst->next;
|
---|
| 581 | else
|
---|
| 582 | instance_list = inst->next;
|
---|
| 583 | if (verbose > 1)
|
---|
| 584 | printf("Finished with %s (exit status %d)\n",
|
---|
| 585 | inst->device, inst->exit_status);
|
---|
| 586 | num_running--;
|
---|
| 587 | return inst;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | #define FLAG_WAIT_ALL 0
|
---|
| 591 | #define FLAG_WAIT_ATLEAST_ONE 1
|
---|
| 592 | /*
|
---|
| 593 | * Wait until all executing child processes have exited; return the
|
---|
| 594 | * logical OR of all of their exit code values.
|
---|
| 595 | */
|
---|
| 596 | static int wait_many(int flags)
|
---|
| 597 | {
|
---|
| 598 | struct fsck_instance *inst;
|
---|
[1765] | 599 | int global_status = 0;
|
---|
| 600 | int wait_flags = 0;
|
---|
[821] | 601 |
|
---|
| 602 | while ((inst = wait_one(wait_flags))) {
|
---|
| 603 | global_status |= inst->exit_status;
|
---|
| 604 | free_instance(inst);
|
---|
| 605 | #ifdef RANDOM_DEBUG
|
---|
| 606 | if (noexecute && (flags & WNOHANG) && !(random() % 3))
|
---|
| 607 | break;
|
---|
| 608 | #endif
|
---|
| 609 | if (flags & FLAG_WAIT_ATLEAST_ONE)
|
---|
| 610 | wait_flags = WNOHANG;
|
---|
| 611 | }
|
---|
| 612 | return global_status;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | /*
|
---|
[1765] | 616 | * Execute a particular fsck program, and link it into the list of
|
---|
| 617 | * child processes we are waiting for.
|
---|
| 618 | */
|
---|
| 619 | static void execute(const char *type, const char *device, const char *mntpt,
|
---|
| 620 | int interactive)
|
---|
| 621 | {
|
---|
| 622 | char *argv[num_args + 4]; /* see count below: */
|
---|
| 623 | int argc;
|
---|
| 624 | int i;
|
---|
| 625 | struct fsck_instance *inst;
|
---|
| 626 | pid_t pid;
|
---|
| 627 |
|
---|
| 628 | inst = xzalloc(sizeof(*inst));
|
---|
| 629 |
|
---|
| 630 | argv[0] = xasprintf("fsck.%s", type); /* 1 */
|
---|
| 631 | for (i = 0; i < num_args; i++)
|
---|
| 632 | argv[i+1] = args[i]; /* num_args */
|
---|
| 633 | argc = num_args + 1;
|
---|
| 634 |
|
---|
| 635 | #if DO_PROGRESS_INDICATOR
|
---|
| 636 | if (progress && !progress_active()) {
|
---|
| 637 | if (strcmp(type, "ext2") == 0
|
---|
| 638 | || strcmp(type, "ext3") == 0
|
---|
| 639 | ) {
|
---|
| 640 | argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
|
---|
| 641 | inst->flags |= FLAG_PROGRESS;
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 | #endif
|
---|
| 645 |
|
---|
| 646 | argv[argc++] = xstrdup(device); /* 1 */
|
---|
| 647 | argv[argc] = NULL; /* 1 */
|
---|
| 648 |
|
---|
| 649 | if (verbose || noexecute) {
|
---|
| 650 | printf("[%s (%d) -- %s]", argv[0], num_running,
|
---|
| 651 | mntpt ? mntpt : device);
|
---|
| 652 | for (i = 0; i < argc; i++)
|
---|
| 653 | printf(" %s", argv[i]);
|
---|
| 654 | puts("");
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | /* Fork and execute the correct program. */
|
---|
| 658 | pid = -1;
|
---|
| 659 | if (!noexecute) {
|
---|
| 660 | pid = spawn(argv);
|
---|
| 661 | if (pid < 0)
|
---|
| 662 | bb_perror_msg("%s", argv[0]);
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | for (i = num_args+1; i < argc; i++)
|
---|
| 666 | free(argv[i]);
|
---|
| 667 |
|
---|
| 668 | inst->pid = pid;
|
---|
| 669 | inst->prog = argv[0];
|
---|
| 670 | inst->type = xstrdup(type);
|
---|
| 671 | inst->device = xstrdup(device);
|
---|
| 672 | inst->base_device = base_device(device);
|
---|
| 673 | inst->start_time = time(NULL);
|
---|
| 674 |
|
---|
| 675 | /* Add to the list of running fsck's.
|
---|
| 676 | * (was adding to the end, but adding to the front is simpler...) */
|
---|
| 677 | inst->next = instance_list;
|
---|
| 678 | instance_list = inst;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | /*
|
---|
[821] | 682 | * Run the fsck program on a particular device
|
---|
| 683 | *
|
---|
| 684 | * If the type is specified using -t, and it isn't prefixed with "no"
|
---|
| 685 | * (as in "noext2") and only one filesystem type is specified, then
|
---|
| 686 | * use that type regardless of what is specified in /etc/fstab.
|
---|
| 687 | *
|
---|
| 688 | * If the type isn't specified by the user, then use either the type
|
---|
[1765] | 689 | * specified in /etc/fstab, or "auto".
|
---|
[821] | 690 | */
|
---|
| 691 | static void fsck_device(struct fs_info *fs, int interactive)
|
---|
| 692 | {
|
---|
| 693 | const char *type;
|
---|
| 694 |
|
---|
[1765] | 695 | if (strcmp(fs->type, "auto") != 0) {
|
---|
[821] | 696 | type = fs->type;
|
---|
[1765] | 697 | if (verbose > 2)
|
---|
| 698 | bb_info_msg("using filesystem type '%s' %s",
|
---|
| 699 | type, "from fstab");
|
---|
| 700 | } else if (fstype
|
---|
| 701 | && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
|
---|
| 702 | && strncmp(fstype, "opts=", 5) != 0
|
---|
| 703 | && strncmp(fstype, "loop", 4) != 0
|
---|
| 704 | && !strchr(fstype, ',')
|
---|
| 705 | ) {
|
---|
[821] | 706 | type = fstype;
|
---|
[1765] | 707 | if (verbose > 2)
|
---|
| 708 | bb_info_msg("using filesystem type '%s' %s",
|
---|
| 709 | type, "from -t");
|
---|
| 710 | } else {
|
---|
| 711 | type = "auto";
|
---|
| 712 | if (verbose > 2)
|
---|
| 713 | bb_info_msg("using filesystem type '%s' %s",
|
---|
| 714 | type, "(default)");
|
---|
| 715 | }
|
---|
[821] | 716 |
|
---|
| 717 | num_running++;
|
---|
[1765] | 718 | execute(type, fs->device, fs->mountpt, interactive);
|
---|
[821] | 719 | }
|
---|
| 720 |
|
---|
| 721 | /*
|
---|
[1765] | 722 | * Returns TRUE if a partition on the same disk is already being
|
---|
| 723 | * checked.
|
---|
[821] | 724 | */
|
---|
[1765] | 725 | static int device_already_active(char *device)
|
---|
| 726 | {
|
---|
| 727 | struct fsck_instance *inst;
|
---|
| 728 | char *base;
|
---|
[821] | 729 |
|
---|
[1765] | 730 | if (force_all_parallel)
|
---|
| 731 | return 0;
|
---|
[821] | 732 |
|
---|
[1765] | 733 | #ifdef BASE_MD
|
---|
| 734 | /* Don't check a soft raid disk with any other disk */
|
---|
| 735 | if (instance_list
|
---|
| 736 | && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
|
---|
| 737 | || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
|
---|
| 738 | ) {
|
---|
| 739 | return 1;
|
---|
| 740 | }
|
---|
| 741 | #endif
|
---|
[821] | 742 |
|
---|
[1765] | 743 | base = base_device(device);
|
---|
| 744 | /*
|
---|
| 745 | * If we don't know the base device, assume that the device is
|
---|
| 746 | * already active if there are any fsck instances running.
|
---|
| 747 | */
|
---|
| 748 | if (!base)
|
---|
| 749 | return (instance_list != NULL);
|
---|
[821] | 750 |
|
---|
[1765] | 751 | for (inst = instance_list; inst; inst = inst->next) {
|
---|
| 752 | if (!inst->base_device || !strcmp(base, inst->base_device)) {
|
---|
| 753 | free(base);
|
---|
| 754 | return 1;
|
---|
[821] | 755 | }
|
---|
| 756 | }
|
---|
| 757 |
|
---|
[1765] | 758 | free(base);
|
---|
| 759 | return 0;
|
---|
[821] | 760 | }
|
---|
| 761 |
|
---|
| 762 | /*
|
---|
| 763 | * This function returns true if a particular option appears in a
|
---|
| 764 | * comma-delimited options list
|
---|
| 765 | */
|
---|
| 766 | static int opt_in_list(char *opt, char *optlist)
|
---|
| 767 | {
|
---|
[1765] | 768 | char *s;
|
---|
| 769 | int len;
|
---|
[821] | 770 |
|
---|
| 771 | if (!optlist)
|
---|
| 772 | return 0;
|
---|
| 773 |
|
---|
[1765] | 774 | len = strlen(opt);
|
---|
| 775 | s = optlist - 1;
|
---|
| 776 | while (1) {
|
---|
| 777 | s = strstr(s + 1, opt);
|
---|
| 778 | if (!s)
|
---|
| 779 | return 0;
|
---|
| 780 | /* neither "opt.." nor "xxx,opt.."? */
|
---|
| 781 | if (s != optlist && s[-1] != ',')
|
---|
| 782 | continue;
|
---|
| 783 | /* neither "..opt" nor "..opt,xxx"? */
|
---|
| 784 | if (s[len] != '\0' && s[len] != ',')
|
---|
| 785 | continue;
|
---|
| 786 | return 1;
|
---|
[821] | 787 | }
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | /* See if the filesystem matches the criteria given by the -t option */
|
---|
[1765] | 791 | static int fs_match(struct fs_info *fs)
|
---|
[821] | 792 | {
|
---|
[1765] | 793 | int n, ret, checked_type;
|
---|
[821] | 794 | char *cp;
|
---|
| 795 |
|
---|
[1765] | 796 | if (!fs_type_list)
|
---|
[821] | 797 | return 1;
|
---|
| 798 |
|
---|
[1765] | 799 | ret = 0;
|
---|
| 800 | checked_type = 0;
|
---|
| 801 | n = 0;
|
---|
| 802 | while (1) {
|
---|
| 803 | cp = fs_type_list[n];
|
---|
| 804 | if (!cp)
|
---|
| 805 | break;
|
---|
| 806 | switch (fs_type_flag[n]) {
|
---|
| 807 | case FS_TYPE_FLAG_NORMAL:
|
---|
[821] | 808 | checked_type++;
|
---|
[1765] | 809 | if (strcmp(cp, fs->type) == 0)
|
---|
[821] | 810 | ret = 1;
|
---|
| 811 | break;
|
---|
[1765] | 812 | case FS_TYPE_FLAG_NEGOPT:
|
---|
[821] | 813 | if (opt_in_list(cp, fs->opts))
|
---|
| 814 | return 0;
|
---|
| 815 | break;
|
---|
[1765] | 816 | case FS_TYPE_FLAG_OPT:
|
---|
[821] | 817 | if (!opt_in_list(cp, fs->opts))
|
---|
| 818 | return 0;
|
---|
| 819 | break;
|
---|
| 820 | }
|
---|
[1765] | 821 | n++;
|
---|
[821] | 822 | }
|
---|
| 823 | if (checked_type == 0)
|
---|
| 824 | return 1;
|
---|
[1765] | 825 |
|
---|
| 826 | return (fs_type_negated ? !ret : ret);
|
---|
[821] | 827 | }
|
---|
| 828 |
|
---|
| 829 | /* Check if we should ignore this filesystem. */
|
---|
| 830 | static int ignore(struct fs_info *fs)
|
---|
| 831 | {
|
---|
| 832 | /*
|
---|
| 833 | * If the pass number is 0, ignore it.
|
---|
| 834 | */
|
---|
| 835 | if (fs->passno == 0)
|
---|
| 836 | return 1;
|
---|
| 837 |
|
---|
| 838 | /*
|
---|
| 839 | * If a specific fstype is specified, and it doesn't match,
|
---|
| 840 | * ignore it.
|
---|
| 841 | */
|
---|
[1765] | 842 | if (!fs_match(fs))
|
---|
| 843 | return 1;
|
---|
[821] | 844 |
|
---|
| 845 | /* Are we ignoring this type? */
|
---|
[1765] | 846 | if (index_in_strings(ignored_types, fs->type) >= 0)
|
---|
[821] | 847 | return 1;
|
---|
| 848 |
|
---|
| 849 | /* We can and want to check this file system type. */
|
---|
| 850 | return 0;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | /* Check all file systems, using the /etc/fstab table. */
|
---|
| 854 | static int check_all(void)
|
---|
| 855 | {
|
---|
[1765] | 856 | struct fs_info *fs;
|
---|
[821] | 857 | int status = EXIT_OK;
|
---|
[1765] | 858 | smallint not_done_yet;
|
---|
| 859 | smallint pass_done;
|
---|
| 860 | int passno;
|
---|
[821] | 861 |
|
---|
| 862 | if (verbose)
|
---|
[1765] | 863 | puts("Checking all filesystems");
|
---|
[821] | 864 |
|
---|
| 865 | /*
|
---|
| 866 | * Do an initial scan over the filesystem; mark filesystems
|
---|
| 867 | * which should be ignored as done, and resolve any "auto"
|
---|
| 868 | * filesystem types (done as a side-effect of calling ignore()).
|
---|
| 869 | */
|
---|
| 870 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 871 | if (ignore(fs))
|
---|
| 872 | fs->flags |= FLAG_DONE;
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | /*
|
---|
| 876 | * Find and check the root filesystem.
|
---|
| 877 | */
|
---|
| 878 | if (!parallel_root) {
|
---|
| 879 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
[1765] | 880 | if (LONE_CHAR(fs->mountpt, '/'))
|
---|
[821] | 881 | break;
|
---|
| 882 | }
|
---|
| 883 | if (fs) {
|
---|
| 884 | if (!skip_root && !ignore(fs)) {
|
---|
| 885 | fsck_device(fs, 1);
|
---|
| 886 | status |= wait_many(FLAG_WAIT_ALL);
|
---|
| 887 | if (status > EXIT_NONDESTRUCT)
|
---|
| 888 | return status;
|
---|
| 889 | }
|
---|
| 890 | fs->flags |= FLAG_DONE;
|
---|
| 891 | }
|
---|
| 892 | }
|
---|
| 893 | /*
|
---|
| 894 | * This is for the bone-headed user who enters the root
|
---|
[1765] | 895 | * filesystem twice. Skip root will skip all root entries.
|
---|
[821] | 896 | */
|
---|
| 897 | if (skip_root)
|
---|
| 898 | for (fs = filesys_info; fs; fs = fs->next)
|
---|
[1765] | 899 | if (LONE_CHAR(fs->mountpt, '/'))
|
---|
[821] | 900 | fs->flags |= FLAG_DONE;
|
---|
| 901 |
|
---|
[1765] | 902 | not_done_yet = 1;
|
---|
| 903 | passno = 1;
|
---|
[821] | 904 | while (not_done_yet) {
|
---|
| 905 | not_done_yet = 0;
|
---|
| 906 | pass_done = 1;
|
---|
| 907 |
|
---|
| 908 | for (fs = filesys_info; fs; fs = fs->next) {
|
---|
| 909 | if (cancel_requested)
|
---|
| 910 | break;
|
---|
| 911 | if (fs->flags & FLAG_DONE)
|
---|
| 912 | continue;
|
---|
| 913 | /*
|
---|
| 914 | * If the filesystem's pass number is higher
|
---|
| 915 | * than the current pass number, then we don't
|
---|
| 916 | * do it yet.
|
---|
| 917 | */
|
---|
| 918 | if (fs->passno > passno) {
|
---|
[1765] | 919 | not_done_yet = 1;
|
---|
[821] | 920 | continue;
|
---|
| 921 | }
|
---|
| 922 | /*
|
---|
| 923 | * If a filesystem on a particular device has
|
---|
| 924 | * already been spawned, then we need to defer
|
---|
| 925 | * this to another pass.
|
---|
| 926 | */
|
---|
| 927 | if (device_already_active(fs->device)) {
|
---|
| 928 | pass_done = 0;
|
---|
| 929 | continue;
|
---|
| 930 | }
|
---|
| 931 | /*
|
---|
| 932 | * Spawn off the fsck process
|
---|
| 933 | */
|
---|
| 934 | fsck_device(fs, serialize);
|
---|
| 935 | fs->flags |= FLAG_DONE;
|
---|
| 936 |
|
---|
| 937 | /*
|
---|
| 938 | * Only do one filesystem at a time, or if we
|
---|
| 939 | * have a limit on the number of fsck's extant
|
---|
| 940 | * at one time, apply that limit.
|
---|
| 941 | */
|
---|
[1765] | 942 | if (serialize
|
---|
| 943 | || (max_running && (num_running >= max_running))
|
---|
| 944 | ) {
|
---|
[821] | 945 | pass_done = 0;
|
---|
| 946 | break;
|
---|
| 947 | }
|
---|
| 948 | }
|
---|
| 949 | if (cancel_requested)
|
---|
| 950 | break;
|
---|
| 951 | if (verbose > 1)
|
---|
| 952 | printf("--waiting-- (pass %d)\n", passno);
|
---|
| 953 | status |= wait_many(pass_done ? FLAG_WAIT_ALL :
|
---|
| 954 | FLAG_WAIT_ATLEAST_ONE);
|
---|
| 955 | if (pass_done) {
|
---|
| 956 | if (verbose > 1)
|
---|
[1765] | 957 | puts("----------------------------------");
|
---|
[821] | 958 | passno++;
|
---|
| 959 | } else
|
---|
[1765] | 960 | not_done_yet = 1;
|
---|
[821] | 961 | }
|
---|
[1765] | 962 | kill_all_if_cancel_requested();
|
---|
[821] | 963 | status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
|
---|
| 964 | return status;
|
---|
| 965 | }
|
---|
| 966 |
|
---|
[1765] | 967 | /*
|
---|
| 968 | * Deal with the fsck -t argument.
|
---|
| 969 | * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
|
---|
| 970 | * Why here we require "-t novfat,nonfs" ??
|
---|
| 971 | */
|
---|
| 972 | static void compile_fs_type(char *fs_type)
|
---|
[821] | 973 | {
|
---|
[1765] | 974 | char *s;
|
---|
| 975 | int num = 2;
|
---|
| 976 | smallint negate;
|
---|
| 977 |
|
---|
| 978 | if (fs_type) {
|
---|
| 979 | s = fs_type;
|
---|
| 980 | while ((s = strchr(s, ','))) {
|
---|
| 981 | num++;
|
---|
| 982 | s++;
|
---|
| 983 | }
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
|
---|
| 987 | fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
|
---|
| 988 | fs_type_negated = -1; /* not yet known is it negated or not */
|
---|
| 989 |
|
---|
| 990 | if (!fs_type)
|
---|
| 991 | return;
|
---|
| 992 |
|
---|
| 993 | num = 0;
|
---|
| 994 | s = fs_type;
|
---|
| 995 | while (1) {
|
---|
| 996 | char *comma;
|
---|
| 997 |
|
---|
| 998 | negate = 0;
|
---|
| 999 | if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
|
---|
| 1000 | s += 2;
|
---|
| 1001 | negate = 1;
|
---|
| 1002 | } else if (s[0] == '!') {
|
---|
| 1003 | s++;
|
---|
| 1004 | negate = 1;
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | if (strcmp(s, "loop") == 0)
|
---|
| 1008 | /* loop is really short-hand for opts=loop */
|
---|
| 1009 | goto loop_special_case;
|
---|
| 1010 | if (strncmp(s, "opts=", 5) == 0) {
|
---|
| 1011 | s += 5;
|
---|
| 1012 | loop_special_case:
|
---|
| 1013 | fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
|
---|
| 1014 | } else {
|
---|
| 1015 | if (fs_type_negated == -1)
|
---|
| 1016 | fs_type_negated = negate;
|
---|
| 1017 | if (fs_type_negated != negate)
|
---|
| 1018 | bb_error_msg_and_die(
|
---|
| 1019 | "either all or none of the filesystem types passed to -t must be prefixed "
|
---|
| 1020 | "with 'no' or '!'");
|
---|
| 1021 | }
|
---|
| 1022 | comma = strchr(s, ',');
|
---|
| 1023 | fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
|
---|
| 1024 | if (!comma)
|
---|
| 1025 | break;
|
---|
| 1026 | s = comma + 1;
|
---|
| 1027 | }
|
---|
[821] | 1028 | }
|
---|
| 1029 |
|
---|
[1765] | 1030 | static void parse_args(int argc, char **argv)
|
---|
[821] | 1031 | {
|
---|
[1765] | 1032 | int i, j;
|
---|
| 1033 | char *arg, *tmp;
|
---|
| 1034 | char *options = NULL;
|
---|
| 1035 | int optpos = 0;
|
---|
| 1036 | int opts_for_fsck = 0;
|
---|
[821] | 1037 |
|
---|
[1765] | 1038 | /* in bss, so already zeroed
|
---|
[821] | 1039 | num_devices = 0;
|
---|
| 1040 | num_args = 0;
|
---|
[1765] | 1041 | instance_list = NULL;
|
---|
| 1042 | */
|
---|
[821] | 1043 |
|
---|
[1765] | 1044 | /* TODO: getopt32 */
|
---|
| 1045 | for (i = 1; i < argc; i++) {
|
---|
[821] | 1046 | arg = argv[i];
|
---|
[1765] | 1047 |
|
---|
| 1048 | /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
|
---|
[821] | 1049 | if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
|
---|
[1765] | 1050 | // FIXME: must check that arg is a blkdev, or resolve
|
---|
| 1051 | // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
|
---|
| 1052 | // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
|
---|
| 1053 | devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
|
---|
| 1054 | devices[num_devices++] = xstrdup(arg);
|
---|
[821] | 1055 | continue;
|
---|
| 1056 | }
|
---|
[1765] | 1057 |
|
---|
[821] | 1058 | if (arg[0] != '-' || opts_for_fsck) {
|
---|
[1765] | 1059 | args = xrealloc(args, (num_args+1) * sizeof(args[0]));
|
---|
| 1060 | args[num_args++] = xstrdup(arg);
|
---|
[821] | 1061 | continue;
|
---|
| 1062 | }
|
---|
[1765] | 1063 |
|
---|
| 1064 | for (j = 1; arg[j]; j++) {
|
---|
[821] | 1065 | if (opts_for_fsck) {
|
---|
[1765] | 1066 | optpos++;
|
---|
| 1067 | /* one extra for '\0' */
|
---|
| 1068 | options = xrealloc(options, optpos + 2);
|
---|
| 1069 | options[optpos] = arg[j];
|
---|
[821] | 1070 | continue;
|
---|
| 1071 | }
|
---|
| 1072 | switch (arg[j]) {
|
---|
| 1073 | case 'A':
|
---|
[1765] | 1074 | doall = 1;
|
---|
[821] | 1075 | break;
|
---|
[1765] | 1076 | #if DO_PROGRESS_INDICATOR
|
---|
[821] | 1077 | case 'C':
|
---|
[1765] | 1078 | progress = 1;
|
---|
| 1079 | if (arg[++j]) { /* -Cn */
|
---|
| 1080 | progress_fd = xatoi_u(&arg[j]);
|
---|
| 1081 | goto next_arg;
|
---|
[821] | 1082 | }
|
---|
[1765] | 1083 | /* -C n */
|
---|
| 1084 | progress_fd = xatoi_u(argv[++i]);
|
---|
| 1085 | goto next_arg;
|
---|
| 1086 | #endif
|
---|
[821] | 1087 | case 'V':
|
---|
| 1088 | verbose++;
|
---|
| 1089 | break;
|
---|
| 1090 | case 'N':
|
---|
[1765] | 1091 | noexecute = 1;
|
---|
[821] | 1092 | break;
|
---|
| 1093 | case 'R':
|
---|
[1765] | 1094 | skip_root = 1;
|
---|
[821] | 1095 | break;
|
---|
| 1096 | case 'T':
|
---|
[1765] | 1097 | notitle = 1;
|
---|
[821] | 1098 | break;
|
---|
[1765] | 1099 | /* case 'M':
|
---|
| 1100 | like_mount = 1;
|
---|
| 1101 | break; */
|
---|
[821] | 1102 | case 'P':
|
---|
[1765] | 1103 | parallel_root = 1;
|
---|
[821] | 1104 | break;
|
---|
| 1105 | case 's':
|
---|
[1765] | 1106 | serialize = 1;
|
---|
[821] | 1107 | break;
|
---|
| 1108 | case 't':
|
---|
| 1109 | if (fstype)
|
---|
| 1110 | bb_show_usage();
|
---|
[1765] | 1111 | if (arg[++j])
|
---|
| 1112 | tmp = &arg[j];
|
---|
| 1113 | else if (++i < argc)
|
---|
| 1114 | tmp = argv[i];
|
---|
[821] | 1115 | else
|
---|
| 1116 | bb_show_usage();
|
---|
[1765] | 1117 | fstype = xstrdup(tmp);
|
---|
| 1118 | compile_fs_type(fstype);
|
---|
[821] | 1119 | goto next_arg;
|
---|
| 1120 | case '-':
|
---|
| 1121 | opts_for_fsck++;
|
---|
| 1122 | break;
|
---|
| 1123 | case '?':
|
---|
| 1124 | bb_show_usage();
|
---|
| 1125 | break;
|
---|
| 1126 | default:
|
---|
[1765] | 1127 | optpos++;
|
---|
| 1128 | /* one extra for '\0' */
|
---|
| 1129 | options = xrealloc(options, optpos + 2);
|
---|
| 1130 | options[optpos] = arg[j];
|
---|
[821] | 1131 | break;
|
---|
| 1132 | }
|
---|
| 1133 | }
|
---|
[1765] | 1134 | next_arg:
|
---|
| 1135 | if (optpos) {
|
---|
[821] | 1136 | options[0] = '-';
|
---|
[1765] | 1137 | options[optpos + 1] = '\0';
|
---|
| 1138 | args = xrealloc(args, (num_args+1) * sizeof(args[0]));
|
---|
| 1139 | args[num_args++] = options;
|
---|
| 1140 | optpos = 0;
|
---|
| 1141 | options = NULL;
|
---|
[821] | 1142 | }
|
---|
| 1143 | }
|
---|
| 1144 | if (getenv("FSCK_FORCE_ALL_PARALLEL"))
|
---|
[1765] | 1145 | force_all_parallel = 1;
|
---|
| 1146 | tmp = getenv("FSCK_MAX_INST");
|
---|
| 1147 | if (tmp)
|
---|
| 1148 | max_running = xatoi(tmp);
|
---|
[821] | 1149 | }
|
---|
| 1150 |
|
---|
[1765] | 1151 | static void signal_cancel(int sig ATTRIBUTE_UNUSED)
|
---|
[821] | 1152 | {
|
---|
[1765] | 1153 | cancel_requested = 1;
|
---|
| 1154 | }
|
---|
| 1155 |
|
---|
| 1156 | int fsck_main(int argc, char **argv);
|
---|
| 1157 | int fsck_main(int argc, char **argv)
|
---|
| 1158 | {
|
---|
[821] | 1159 | int i, status = 0;
|
---|
[1765] | 1160 | int interactive;
|
---|
[821] | 1161 | const char *fstab;
|
---|
| 1162 | struct fs_info *fs;
|
---|
[1765] | 1163 | struct sigaction sa;
|
---|
[821] | 1164 |
|
---|
[1765] | 1165 | memset(&sa, 0, sizeof(sa));
|
---|
| 1166 | sa.sa_handler = signal_cancel;
|
---|
| 1167 | sigaction(SIGINT, &sa, 0);
|
---|
| 1168 | sigaction(SIGTERM, &sa, 0);
|
---|
[821] | 1169 |
|
---|
[1765] | 1170 | setbuf(stdout, NULL);
|
---|
[821] | 1171 |
|
---|
[1765] | 1172 | parse_args(argc, argv);
|
---|
| 1173 |
|
---|
[821] | 1174 | if (!notitle)
|
---|
[1765] | 1175 | puts("fsck (busybox "BB_VER", "BB_BT")");
|
---|
[821] | 1176 |
|
---|
[1765] | 1177 | /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
|
---|
| 1178 | * so we are scanning it anyway */
|
---|
[821] | 1179 | fstab = getenv("FSTAB_FILE");
|
---|
| 1180 | if (!fstab)
|
---|
[1765] | 1181 | fstab = "/etc/fstab";
|
---|
[821] | 1182 | load_fs_info(fstab);
|
---|
| 1183 |
|
---|
[1765] | 1184 | interactive = (num_devices == 1) | serialize;
|
---|
[821] | 1185 |
|
---|
| 1186 | /* If -A was specified ("check all"), do that! */
|
---|
| 1187 | if (doall)
|
---|
| 1188 | return check_all();
|
---|
| 1189 |
|
---|
| 1190 | if (num_devices == 0) {
|
---|
[1765] | 1191 | serialize = 1;
|
---|
| 1192 | interactive = 1;
|
---|
[821] | 1193 | return check_all();
|
---|
| 1194 | }
|
---|
[1765] | 1195 |
|
---|
| 1196 | for (i = 0; i < num_devices; i++) {
|
---|
[821] | 1197 | if (cancel_requested) {
|
---|
[1765] | 1198 | kill_all_if_cancel_requested();
|
---|
[821] | 1199 | break;
|
---|
| 1200 | }
|
---|
[1765] | 1201 |
|
---|
[821] | 1202 | fs = lookup(devices[i]);
|
---|
[1765] | 1203 | if (!fs)
|
---|
| 1204 | fs = create_fs_device(devices[i], 0, "auto", 0, -1, -1);
|
---|
[821] | 1205 | fsck_device(fs, interactive);
|
---|
[1765] | 1206 |
|
---|
| 1207 | if (serialize
|
---|
| 1208 | || (max_running && (num_running >= max_running))
|
---|
| 1209 | ) {
|
---|
[821] | 1210 | struct fsck_instance *inst;
|
---|
| 1211 |
|
---|
| 1212 | inst = wait_one(0);
|
---|
| 1213 | if (inst) {
|
---|
| 1214 | status |= inst->exit_status;
|
---|
| 1215 | free_instance(inst);
|
---|
| 1216 | }
|
---|
| 1217 | if (verbose > 1)
|
---|
[1765] | 1218 | puts("----------------------------------");
|
---|
[821] | 1219 | }
|
---|
| 1220 | }
|
---|
| 1221 | status |= wait_many(FLAG_WAIT_ALL);
|
---|
| 1222 | return status;
|
---|
| 1223 | }
|
---|