Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 49 | #include "libbb.h" |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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 | */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 118 | static const char *const devfs_hier[] = { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 127 | const char *const *hier; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 229 | static const char *const ignored_types[] = { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 230 | "ignore", |
| 231 | "iso9660", |
| 232 | "nfs", |
| 233 | "proc", |
| 234 | "sw", |
| 235 | "swap", |
| 236 | "tmpfs", |
| 237 | "devpts", |
| 238 | NULL |
| 239 | }; |
| 240 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 241 | static const char *const really_wanted[] = { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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; |
Denis Vlasenko | c4523c2 | 2008-03-28 02:24:59 +0000 | [diff] [blame] | 288 | ret = xstrdup(s); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | f90ab18 | 2008-03-20 21:19:35 +0000 | [diff] [blame] | 480 | if ((f = fopen_or_warn(filename, "r")) == NULL) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | while (!feof(f)) { |
| 484 | lineno++; |
| 485 | if (!fgets(buf, sizeof(buf), f)) |
| 486 | break; |
| 487 | buf[sizeof(buf)-1] = 0; |
| 488 | if (parse_fstab_line(buf, &fs) < 0) { |
| 489 | bb_error_msg("WARNING: bad format " |
| 490 | "on line %d of %s\n", lineno, filename); |
| 491 | continue; |
| 492 | } |
| 493 | if (!fs) |
| 494 | continue; |
| 495 | if (fs->passno < 0) |
| 496 | fs->passno = 0; |
| 497 | else |
| 498 | old_fstab = 0; |
| 499 | } |
| 500 | |
| 501 | fclose(f); |
| 502 | |
| 503 | if (old_fstab) { |
| 504 | fputs("\007\007\007" |
| 505 | "WARNING: Your /etc/fstab does not contain the fsck passno\n" |
| 506 | " field. I will kludge around things for you, but you\n" |
| 507 | " should fix your /etc/fstab file as soon as you can.\n\n", stderr); |
| 508 | |
| 509 | for (fs = filesys_info; fs; fs = fs->next) { |
| 510 | fs->passno = 1; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /* Lookup filesys in /etc/fstab and return the corresponding entry. */ |
| 516 | static struct fs_info *lookup(char *filesys) |
| 517 | { |
| 518 | struct fs_info *fs; |
| 519 | |
| 520 | /* No filesys name given. */ |
| 521 | if (filesys == NULL) |
| 522 | return NULL; |
| 523 | |
| 524 | for (fs = filesys_info; fs; fs = fs->next) { |
| 525 | if (!strcmp(filesys, fs->device) || |
| 526 | (fs->mountpt && !strcmp(filesys, fs->mountpt))) |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | return fs; |
| 531 | } |
| 532 | |
| 533 | /* Find fsck program for a given fs type. */ |
| 534 | static char *find_fsck(char *type) |
| 535 | { |
| 536 | char *s; |
| 537 | const char *tpl; |
| 538 | char *p = string_copy(fsck_path); |
| 539 | struct stat st; |
| 540 | |
| 541 | /* Are we looking for a program or just a type? */ |
| 542 | tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s"); |
| 543 | |
| 544 | for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) { |
| 545 | s = xasprintf(tpl, s, type); |
| 546 | if (stat(s, &st) == 0) break; |
| 547 | free(s); |
| 548 | } |
| 549 | free(p); |
| 550 | return s; |
| 551 | } |
| 552 | |
| 553 | static int progress_active(void) |
| 554 | { |
| 555 | struct fsck_instance *inst; |
| 556 | |
| 557 | for (inst = instance_list; inst; inst = inst->next) { |
| 558 | if (inst->flags & FLAG_DONE) |
| 559 | continue; |
| 560 | if (inst->flags & FLAG_PROGRESS) |
| 561 | return 1; |
| 562 | } |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Execute a particular fsck program, and link it into the list of |
| 568 | * child processes we are waiting for. |
| 569 | */ |
| 570 | static int execute(const char *type, const char *device, const char *mntpt, |
| 571 | int interactive) |
| 572 | { |
| 573 | char *s, *argv[80]; |
| 574 | char *prog; |
| 575 | int argc, i; |
| 576 | struct fsck_instance *inst, *p; |
| 577 | pid_t pid; |
| 578 | |
| 579 | inst = malloc(sizeof(struct fsck_instance)); |
| 580 | if (!inst) |
| 581 | return ENOMEM; |
| 582 | memset(inst, 0, sizeof(struct fsck_instance)); |
| 583 | |
| 584 | prog = xasprintf("fsck.%s", type); |
| 585 | argv[0] = prog; |
| 586 | argc = 1; |
| 587 | |
| 588 | for (i=0; i <num_args; i++) |
| 589 | argv[argc++] = string_copy(args[i]); |
| 590 | |
| 591 | if (progress && !progress_active()) { |
| 592 | if ((strcmp(type, "ext2") == 0) || |
| 593 | (strcmp(type, "ext3") == 0)) { |
| 594 | char tmp[80]; |
| 595 | snprintf(tmp, 80, "-C%d", progress_fd); |
| 596 | argv[argc++] = string_copy(tmp); |
| 597 | inst->flags |= FLAG_PROGRESS; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | argv[argc++] = string_copy(device); |
| 602 | argv[argc] = 0; |
| 603 | |
| 604 | s = find_fsck(prog); |
| 605 | if (s == NULL) { |
| 606 | bb_error_msg("%s: not found", prog); |
| 607 | return ENOENT; |
| 608 | } |
| 609 | |
| 610 | if (verbose || noexecute) { |
| 611 | printf("[%s (%d) -- %s] ", s, num_running, |
| 612 | mntpt ? mntpt : device); |
| 613 | for (i=0; i < argc; i++) |
| 614 | printf("%s ", argv[i]); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 615 | bb_putchar('\n'); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* Fork and execute the correct program. */ |
| 619 | if (noexecute) |
| 620 | pid = -1; |
| 621 | else if ((pid = fork()) < 0) { |
| 622 | perror("fork"); |
| 623 | return errno; |
| 624 | } else if (pid == 0) { |
| 625 | if (!interactive) |
| 626 | close(0); |
| 627 | (void) execv(s, argv); |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 628 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | for (i = 1; i < argc; i++) |
| 632 | free(argv[i]); |
| 633 | |
| 634 | free(s); |
| 635 | inst->pid = pid; |
| 636 | inst->prog = prog; |
| 637 | inst->type = string_copy(type); |
| 638 | inst->device = string_copy(device); |
| 639 | inst->base_device = base_device(device); |
| 640 | inst->start_time = time(0); |
| 641 | inst->next = NULL; |
| 642 | |
| 643 | /* |
| 644 | * Find the end of the list, so we add the instance on at the end. |
| 645 | */ |
| 646 | for (p = instance_list; p && p->next; p = p->next); |
| 647 | |
| 648 | if (p) |
| 649 | p->next = inst; |
| 650 | else |
| 651 | instance_list = inst; |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Send a signal to all outstanding fsck child processes |
| 658 | */ |
| 659 | static int kill_all(int signum) |
| 660 | { |
| 661 | struct fsck_instance *inst; |
| 662 | int n = 0; |
| 663 | |
| 664 | for (inst = instance_list; inst; inst = inst->next) { |
| 665 | if (inst->flags & FLAG_DONE) |
| 666 | continue; |
| 667 | kill(inst->pid, signum); |
| 668 | n++; |
| 669 | } |
| 670 | return n; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Wait for one child process to exit; when it does, unlink it from |
| 675 | * the list of executing child processes, and return it. |
| 676 | */ |
| 677 | static struct fsck_instance *wait_one(int flags) |
| 678 | { |
| 679 | int status; |
| 680 | int sig; |
| 681 | struct fsck_instance *inst, *inst2, *prev; |
| 682 | pid_t pid; |
| 683 | |
| 684 | if (!instance_list) |
| 685 | return NULL; |
| 686 | |
| 687 | if (noexecute) { |
| 688 | inst = instance_list; |
| 689 | prev = 0; |
| 690 | #ifdef RANDOM_DEBUG |
| 691 | while (inst->next && (random() & 1)) { |
| 692 | prev = inst; |
| 693 | inst = inst->next; |
| 694 | } |
| 695 | #endif |
| 696 | inst->exit_status = 0; |
| 697 | goto ret_inst; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * gcc -Wall fails saving throw against stupidity |
| 702 | * (inst and prev are thought to be uninitialized variables) |
| 703 | */ |
| 704 | inst = prev = NULL; |
| 705 | |
| 706 | do { |
| 707 | pid = waitpid(-1, &status, flags); |
| 708 | if (cancel_requested && !kill_sent) { |
| 709 | kill_all(SIGTERM); |
| 710 | kill_sent++; |
| 711 | } |
| 712 | if ((pid == 0) && (flags & WNOHANG)) |
| 713 | return NULL; |
| 714 | if (pid < 0) { |
| 715 | if ((errno == EINTR) || (errno == EAGAIN)) |
| 716 | continue; |
| 717 | if (errno == ECHILD) { |
| 718 | bb_error_msg("wait: no more child process?!?"); |
| 719 | return NULL; |
| 720 | } |
| 721 | perror("wait"); |
| 722 | continue; |
| 723 | } |
| 724 | for (prev = 0, inst = instance_list; |
| 725 | inst; |
| 726 | prev = inst, inst = inst->next) { |
| 727 | if (inst->pid == pid) |
| 728 | break; |
| 729 | } |
| 730 | } while (!inst); |
| 731 | |
| 732 | if (WIFEXITED(status)) |
| 733 | status = WEXITSTATUS(status); |
| 734 | else if (WIFSIGNALED(status)) { |
| 735 | sig = WTERMSIG(status); |
| 736 | if (sig == SIGINT) { |
| 737 | status = EXIT_UNCORRECTED; |
| 738 | } else { |
| 739 | printf("Warning... %s for device %s exited " |
| 740 | "with signal %d.\n", |
| 741 | inst->prog, inst->device, sig); |
| 742 | status = EXIT_ERROR; |
| 743 | } |
| 744 | } else { |
| 745 | printf("%s %s: status is %x, should never happen.\n", |
| 746 | inst->prog, inst->device, status); |
| 747 | status = EXIT_ERROR; |
| 748 | } |
| 749 | inst->exit_status = status; |
| 750 | if (progress && (inst->flags & FLAG_PROGRESS) && |
| 751 | !progress_active()) { |
| 752 | for (inst2 = instance_list; inst2; inst2 = inst2->next) { |
| 753 | if (inst2->flags & FLAG_DONE) |
| 754 | continue; |
| 755 | if (strcmp(inst2->type, "ext2") && |
| 756 | strcmp(inst2->type, "ext3")) |
| 757 | continue; |
| 758 | /* |
| 759 | * If we've just started the fsck, wait a tiny |
| 760 | * bit before sending the kill, to give it |
| 761 | * time to set up the signal handler |
| 762 | */ |
| 763 | if (inst2->start_time < time(0)+2) { |
| 764 | if (fork() == 0) { |
| 765 | sleep(1); |
| 766 | kill(inst2->pid, SIGUSR1); |
| 767 | exit(0); |
| 768 | } |
| 769 | } else |
| 770 | kill(inst2->pid, SIGUSR1); |
| 771 | inst2->flags |= FLAG_PROGRESS; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | ret_inst: |
| 776 | if (prev) |
| 777 | prev->next = inst->next; |
| 778 | else |
| 779 | instance_list = inst->next; |
| 780 | if (verbose > 1) |
| 781 | printf("Finished with %s (exit status %d)\n", |
| 782 | inst->device, inst->exit_status); |
| 783 | num_running--; |
| 784 | return inst; |
| 785 | } |
| 786 | |
| 787 | #define FLAG_WAIT_ALL 0 |
| 788 | #define FLAG_WAIT_ATLEAST_ONE 1 |
| 789 | /* |
| 790 | * Wait until all executing child processes have exited; return the |
| 791 | * logical OR of all of their exit code values. |
| 792 | */ |
| 793 | static int wait_many(int flags) |
| 794 | { |
| 795 | struct fsck_instance *inst; |
| 796 | int global_status = 0; |
| 797 | int wait_flags = 0; |
| 798 | |
| 799 | while ((inst = wait_one(wait_flags))) { |
| 800 | global_status |= inst->exit_status; |
| 801 | free_instance(inst); |
| 802 | #ifdef RANDOM_DEBUG |
| 803 | if (noexecute && (flags & WNOHANG) && !(random() % 3)) |
| 804 | break; |
| 805 | #endif |
| 806 | if (flags & FLAG_WAIT_ATLEAST_ONE) |
| 807 | wait_flags = WNOHANG; |
| 808 | } |
| 809 | return global_status; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Run the fsck program on a particular device |
| 814 | * |
| 815 | * If the type is specified using -t, and it isn't prefixed with "no" |
| 816 | * (as in "noext2") and only one filesystem type is specified, then |
| 817 | * use that type regardless of what is specified in /etc/fstab. |
| 818 | * |
| 819 | * If the type isn't specified by the user, then use either the type |
| 820 | * specified in /etc/fstab, or DEFAULT_FSTYPE. |
| 821 | */ |
| 822 | static void fsck_device(struct fs_info *fs, int interactive) |
| 823 | { |
| 824 | const char *type; |
| 825 | int retval; |
| 826 | |
| 827 | interpret_type(fs); |
| 828 | |
| 829 | if (strcmp(fs->type, "auto") != 0) |
| 830 | type = fs->type; |
| 831 | else if (fstype && strncmp(fstype, "no", 2) && |
| 832 | strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) && |
| 833 | !strchr(fstype, ',')) |
| 834 | type = fstype; |
| 835 | else |
| 836 | type = DEFAULT_FSTYPE; |
| 837 | |
| 838 | num_running++; |
| 839 | retval = execute(type, fs->device, fs->mountpt, interactive); |
| 840 | if (retval) { |
| 841 | bb_error_msg("error %d while executing fsck.%s for %s", |
| 842 | retval, type, fs->device); |
| 843 | num_running--; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /* |
| 849 | * Deal with the fsck -t argument. |
| 850 | */ |
| 851 | struct fs_type_compile { |
| 852 | char **list; |
| 853 | int *type; |
| 854 | int negate; |
| 855 | } fs_type_compiled; |
| 856 | |
| 857 | #define FS_TYPE_NORMAL 0 |
| 858 | #define FS_TYPE_OPT 1 |
| 859 | #define FS_TYPE_NEGOPT 2 |
| 860 | |
| 861 | static const char fs_type_syntax_error[] = |
| 862 | "Either all or none of the filesystem types passed to -t must be prefixed\n" |
| 863 | "with 'no' or '!'."; |
| 864 | |
| 865 | static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp) |
| 866 | { |
| 867 | char *cp, *list, *s; |
| 868 | int num = 2; |
| 869 | int negate, first_negate = 1; |
| 870 | |
| 871 | if (fs_type) { |
| 872 | for (cp=fs_type; *cp; cp++) { |
| 873 | if (*cp == ',') |
| 874 | num++; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | cmp->list = xzalloc(num * sizeof(char *)); |
| 879 | cmp->type = xzalloc(num * sizeof(int)); |
| 880 | cmp->negate = 0; |
| 881 | |
| 882 | if (!fs_type) |
| 883 | return; |
| 884 | |
| 885 | list = string_copy(fs_type); |
| 886 | num = 0; |
| 887 | s = strtok(list, ","); |
| 888 | while(s) { |
| 889 | negate = 0; |
| 890 | if (strncmp(s, "no", 2) == 0) { |
| 891 | s += 2; |
| 892 | negate = 1; |
| 893 | } else if (*s == '!') { |
| 894 | s++; |
| 895 | negate = 1; |
| 896 | } |
| 897 | if (strcmp(s, "loop") == 0) |
| 898 | /* loop is really short-hand for opts=loop */ |
| 899 | goto loop_special_case; |
| 900 | else if (strncmp(s, "opts=", 5) == 0) { |
| 901 | s += 5; |
| 902 | loop_special_case: |
| 903 | cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT; |
| 904 | } else { |
| 905 | if (first_negate) { |
| 906 | cmp->negate = negate; |
| 907 | first_negate = 0; |
| 908 | } |
| 909 | if ((negate && !cmp->negate) || |
| 910 | (!negate && cmp->negate)) { |
| 911 | bb_error_msg_and_die("%s", fs_type_syntax_error); |
| 912 | } |
| 913 | } |
| 914 | cmp->list[num++] = string_copy(s); |
| 915 | s = strtok(NULL, ","); |
| 916 | } |
| 917 | free(list); |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * This function returns true if a particular option appears in a |
| 922 | * comma-delimited options list |
| 923 | */ |
| 924 | static int opt_in_list(char *opt, char *optlist) |
| 925 | { |
| 926 | char *list, *s; |
| 927 | |
| 928 | if (!optlist) |
| 929 | return 0; |
| 930 | list = string_copy(optlist); |
| 931 | |
| 932 | s = strtok(list, ","); |
| 933 | while(s) { |
| 934 | if (strcmp(s, opt) == 0) { |
| 935 | free(list); |
| 936 | return 1; |
| 937 | } |
| 938 | s = strtok(NULL, ","); |
| 939 | } |
| 940 | free(list); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | /* See if the filesystem matches the criteria given by the -t option */ |
| 945 | static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp) |
| 946 | { |
| 947 | int n, ret = 0, checked_type = 0; |
| 948 | char *cp; |
| 949 | |
| 950 | if (cmp->list == 0 || cmp->list[0] == 0) |
| 951 | return 1; |
| 952 | |
| 953 | for (n=0; (cp = cmp->list[n]); n++) { |
| 954 | switch (cmp->type[n]) { |
| 955 | case FS_TYPE_NORMAL: |
| 956 | checked_type++; |
| 957 | if (strcmp(cp, fs->type) == 0) { |
| 958 | ret = 1; |
| 959 | } |
| 960 | break; |
| 961 | case FS_TYPE_NEGOPT: |
| 962 | if (opt_in_list(cp, fs->opts)) |
| 963 | return 0; |
| 964 | break; |
| 965 | case FS_TYPE_OPT: |
| 966 | if (!opt_in_list(cp, fs->opts)) |
| 967 | return 0; |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | if (checked_type == 0) |
| 972 | return 1; |
| 973 | return (cmp->negate ? !ret : ret); |
| 974 | } |
| 975 | |
| 976 | /* Check if we should ignore this filesystem. */ |
| 977 | static int ignore(struct fs_info *fs) |
| 978 | { |
| 979 | int wanted; |
| 980 | char *s; |
| 981 | |
| 982 | /* |
| 983 | * If the pass number is 0, ignore it. |
| 984 | */ |
| 985 | if (fs->passno == 0) |
| 986 | return 1; |
| 987 | |
| 988 | interpret_type(fs); |
| 989 | |
| 990 | /* |
| 991 | * If a specific fstype is specified, and it doesn't match, |
| 992 | * ignore it. |
| 993 | */ |
| 994 | if (!fs_match(fs, &fs_type_compiled)) return 1; |
| 995 | |
| 996 | /* Are we ignoring this type? */ |
| 997 | if (index_in_str_array(ignored_types, fs->type) >= 0) |
| 998 | return 1; |
| 999 | |
| 1000 | /* Do we really really want to check this fs? */ |
| 1001 | wanted = index_in_str_array(really_wanted, fs->type) >= 0; |
| 1002 | |
| 1003 | /* See if the <fsck.fs> program is available. */ |
| 1004 | s = find_fsck(fs->type); |
| 1005 | if (s == NULL) { |
| 1006 | if (wanted) |
| 1007 | bb_error_msg("cannot check %s: fsck.%s not found", |
| 1008 | fs->device, fs->type); |
| 1009 | return 1; |
| 1010 | } |
| 1011 | free(s); |
| 1012 | |
| 1013 | /* We can and want to check this file system type. */ |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Returns TRUE if a partition on the same disk is already being |
| 1019 | * checked. |
| 1020 | */ |
| 1021 | static int device_already_active(char *device) |
| 1022 | { |
| 1023 | struct fsck_instance *inst; |
| 1024 | char *base; |
| 1025 | |
| 1026 | if (force_all_parallel) |
| 1027 | return 0; |
| 1028 | |
| 1029 | #ifdef BASE_MD |
| 1030 | /* Don't check a soft raid disk with any other disk */ |
| 1031 | if (instance_list && |
| 1032 | (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) || |
| 1033 | !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))) |
| 1034 | return 1; |
| 1035 | #endif |
| 1036 | |
| 1037 | base = base_device(device); |
| 1038 | /* |
| 1039 | * If we don't know the base device, assume that the device is |
| 1040 | * already active if there are any fsck instances running. |
| 1041 | */ |
| 1042 | if (!base) |
| 1043 | return (instance_list != 0); |
| 1044 | for (inst = instance_list; inst; inst = inst->next) { |
| 1045 | if (!inst->base_device || !strcmp(base, inst->base_device)) { |
| 1046 | free(base); |
| 1047 | return 1; |
| 1048 | } |
| 1049 | } |
| 1050 | free(base); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | /* Check all file systems, using the /etc/fstab table. */ |
| 1055 | static int check_all(void) |
| 1056 | { |
| 1057 | struct fs_info *fs = NULL; |
| 1058 | int status = EXIT_OK; |
| 1059 | int not_done_yet = 1; |
| 1060 | int passno = 1; |
| 1061 | int pass_done; |
| 1062 | |
| 1063 | if (verbose) |
| 1064 | fputs("Checking all file systems.\n", stdout); |
| 1065 | |
| 1066 | /* |
| 1067 | * Do an initial scan over the filesystem; mark filesystems |
| 1068 | * which should be ignored as done, and resolve any "auto" |
| 1069 | * filesystem types (done as a side-effect of calling ignore()). |
| 1070 | */ |
| 1071 | for (fs = filesys_info; fs; fs = fs->next) { |
| 1072 | if (ignore(fs)) |
| 1073 | fs->flags |= FLAG_DONE; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | * Find and check the root filesystem. |
| 1078 | */ |
| 1079 | if (!parallel_root) { |
| 1080 | for (fs = filesys_info; fs; fs = fs->next) { |
| 1081 | if (LONE_CHAR(fs->mountpt, '/')) |
| 1082 | break; |
| 1083 | } |
| 1084 | if (fs) { |
| 1085 | if (!skip_root && !ignore(fs)) { |
| 1086 | fsck_device(fs, 1); |
| 1087 | status |= wait_many(FLAG_WAIT_ALL); |
| 1088 | if (status > EXIT_NONDESTRUCT) |
| 1089 | return status; |
| 1090 | } |
| 1091 | fs->flags |= FLAG_DONE; |
| 1092 | } |
| 1093 | } |
| 1094 | /* |
| 1095 | * This is for the bone-headed user who enters the root |
| 1096 | * filesystem twice. Skip root will skep all root entries. |
| 1097 | */ |
| 1098 | if (skip_root) |
| 1099 | for (fs = filesys_info; fs; fs = fs->next) |
| 1100 | if (LONE_CHAR(fs->mountpt, '/')) |
| 1101 | fs->flags |= FLAG_DONE; |
| 1102 | |
| 1103 | while (not_done_yet) { |
| 1104 | not_done_yet = 0; |
| 1105 | pass_done = 1; |
| 1106 | |
| 1107 | for (fs = filesys_info; fs; fs = fs->next) { |
| 1108 | if (cancel_requested) |
| 1109 | break; |
| 1110 | if (fs->flags & FLAG_DONE) |
| 1111 | continue; |
| 1112 | /* |
| 1113 | * If the filesystem's pass number is higher |
| 1114 | * than the current pass number, then we don't |
| 1115 | * do it yet. |
| 1116 | */ |
| 1117 | if (fs->passno > passno) { |
| 1118 | not_done_yet++; |
| 1119 | continue; |
| 1120 | } |
| 1121 | /* |
| 1122 | * If a filesystem on a particular device has |
| 1123 | * already been spawned, then we need to defer |
| 1124 | * this to another pass. |
| 1125 | */ |
| 1126 | if (device_already_active(fs->device)) { |
| 1127 | pass_done = 0; |
| 1128 | continue; |
| 1129 | } |
| 1130 | /* |
| 1131 | * Spawn off the fsck process |
| 1132 | */ |
| 1133 | fsck_device(fs, serialize); |
| 1134 | fs->flags |= FLAG_DONE; |
| 1135 | |
| 1136 | /* |
| 1137 | * Only do one filesystem at a time, or if we |
| 1138 | * have a limit on the number of fsck's extant |
| 1139 | * at one time, apply that limit. |
| 1140 | */ |
| 1141 | if (serialize || |
| 1142 | (max_running && (num_running >= max_running))) { |
| 1143 | pass_done = 0; |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | if (cancel_requested) |
| 1148 | break; |
| 1149 | if (verbose > 1) |
| 1150 | printf("--waiting-- (pass %d)\n", passno); |
| 1151 | status |= wait_many(pass_done ? FLAG_WAIT_ALL : |
| 1152 | FLAG_WAIT_ATLEAST_ONE); |
| 1153 | if (pass_done) { |
| 1154 | if (verbose > 1) |
| 1155 | printf("----------------------------------\n"); |
| 1156 | passno++; |
| 1157 | } else |
| 1158 | not_done_yet++; |
| 1159 | } |
| 1160 | if (cancel_requested && !kill_sent) { |
| 1161 | kill_all(SIGTERM); |
| 1162 | kill_sent++; |
| 1163 | } |
| 1164 | status |= wait_many(FLAG_WAIT_ATLEAST_ONE); |
| 1165 | return status; |
| 1166 | } |
| 1167 | |
| 1168 | static void signal_cancel(int sig FSCK_ATTR((unused))) |
| 1169 | { |
| 1170 | cancel_requested++; |
| 1171 | } |
| 1172 | |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 1173 | static void PRS(int argc, char **argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1174 | { |
| 1175 | int i, j; |
| 1176 | char *arg, *dev, *tmp = 0; |
| 1177 | char options[128]; |
| 1178 | int opt = 0; |
| 1179 | int opts_for_fsck = 0; |
| 1180 | struct sigaction sa; |
| 1181 | |
| 1182 | /* |
| 1183 | * Set up signal action |
| 1184 | */ |
| 1185 | memset(&sa, 0, sizeof(struct sigaction)); |
| 1186 | sa.sa_handler = signal_cancel; |
| 1187 | sigaction(SIGINT, &sa, 0); |
| 1188 | sigaction(SIGTERM, &sa, 0); |
| 1189 | |
| 1190 | num_devices = 0; |
| 1191 | num_args = 0; |
| 1192 | instance_list = 0; |
| 1193 | |
| 1194 | for (i=1; i < argc; i++) { |
| 1195 | arg = argv[i]; |
| 1196 | if (!arg) |
| 1197 | continue; |
| 1198 | if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) { |
| 1199 | if (num_devices >= MAX_DEVICES) { |
| 1200 | bb_error_msg_and_die("too many devices"); |
| 1201 | } |
| 1202 | dev = blkid_get_devname(cache, arg, NULL); |
| 1203 | if (!dev && strchr(arg, '=')) { |
| 1204 | /* |
| 1205 | * Check to see if we failed because |
| 1206 | * /proc/partitions isn't found. |
| 1207 | */ |
| 1208 | if (access("/proc/partitions", R_OK) < 0) { |
| 1209 | bb_perror_msg_and_die("cannot open /proc/partitions " |
| 1210 | "(is /proc mounted?)"); |
| 1211 | } |
| 1212 | /* |
| 1213 | * Check to see if this is because |
| 1214 | * we're not running as root |
| 1215 | */ |
| 1216 | if (geteuid()) |
| 1217 | bb_error_msg_and_die( |
| 1218 | "must be root to scan for matching filesystems: %s\n", arg); |
| 1219 | else |
| 1220 | bb_error_msg_and_die( |
| 1221 | "cannot find matching filesystem: %s", arg); |
| 1222 | } |
| 1223 | devices[num_devices++] = dev ? dev : string_copy(arg); |
| 1224 | continue; |
| 1225 | } |
| 1226 | if (arg[0] != '-' || opts_for_fsck) { |
| 1227 | if (num_args >= MAX_ARGS) { |
| 1228 | bb_error_msg_and_die("too many arguments"); |
| 1229 | } |
| 1230 | args[num_args++] = string_copy(arg); |
| 1231 | continue; |
| 1232 | } |
| 1233 | for (j=1; arg[j]; j++) { |
| 1234 | if (opts_for_fsck) { |
| 1235 | options[++opt] = arg[j]; |
| 1236 | continue; |
| 1237 | } |
| 1238 | switch (arg[j]) { |
| 1239 | case 'A': |
| 1240 | doall++; |
| 1241 | break; |
| 1242 | case 'C': |
| 1243 | progress++; |
| 1244 | if (arg[j+1]) { |
| 1245 | progress_fd = string_to_int(arg+j+1); |
| 1246 | if (progress_fd < 0) |
| 1247 | progress_fd = 0; |
| 1248 | else |
| 1249 | goto next_arg; |
| 1250 | } else if ((i+1) < argc |
| 1251 | && argv[i+1][0] != '-') { |
| 1252 | progress_fd = string_to_int(argv[i]); |
| 1253 | if (progress_fd < 0) |
| 1254 | progress_fd = 0; |
| 1255 | else { |
| 1256 | goto next_arg; |
| 1257 | i++; |
| 1258 | } |
| 1259 | } |
| 1260 | break; |
| 1261 | case 'V': |
| 1262 | verbose++; |
| 1263 | break; |
| 1264 | case 'N': |
| 1265 | noexecute++; |
| 1266 | break; |
| 1267 | case 'R': |
| 1268 | skip_root++; |
| 1269 | break; |
| 1270 | case 'T': |
| 1271 | notitle++; |
| 1272 | break; |
| 1273 | case 'M': |
| 1274 | like_mount++; |
| 1275 | break; |
| 1276 | case 'P': |
| 1277 | parallel_root++; |
| 1278 | break; |
| 1279 | case 's': |
| 1280 | serialize++; |
| 1281 | break; |
| 1282 | case 't': |
| 1283 | tmp = 0; |
| 1284 | if (fstype) |
| 1285 | bb_show_usage(); |
| 1286 | if (arg[j+1]) |
| 1287 | tmp = arg+j+1; |
| 1288 | else if ((i+1) < argc) |
| 1289 | tmp = argv[++i]; |
| 1290 | else |
| 1291 | bb_show_usage(); |
| 1292 | fstype = string_copy(tmp); |
| 1293 | compile_fs_type(fstype, &fs_type_compiled); |
| 1294 | goto next_arg; |
| 1295 | case '-': |
| 1296 | opts_for_fsck++; |
| 1297 | break; |
| 1298 | case '?': |
| 1299 | bb_show_usage(); |
| 1300 | break; |
| 1301 | default: |
| 1302 | options[++opt] = arg[j]; |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | next_arg: |
| 1307 | if (opt) { |
| 1308 | options[0] = '-'; |
| 1309 | options[++opt] = '\0'; |
| 1310 | if (num_args >= MAX_ARGS) { |
| 1311 | bb_error_msg("too many arguments"); |
| 1312 | } |
| 1313 | args[num_args++] = string_copy(options); |
| 1314 | opt = 0; |
| 1315 | } |
| 1316 | } |
| 1317 | if (getenv("FSCK_FORCE_ALL_PARALLEL")) |
| 1318 | force_all_parallel++; |
| 1319 | if ((tmp = getenv("FSCK_MAX_INST"))) |
| 1320 | max_running = atoi(tmp); |
| 1321 | } |
| 1322 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1323 | int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 1324 | int fsck_main(int argc, char **argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1325 | { |
| 1326 | int i, status = 0; |
| 1327 | int interactive = 0; |
| 1328 | const char *fstab; |
| 1329 | struct fs_info *fs; |
| 1330 | |
| 1331 | setvbuf(stdout, NULL, _IONBF, BUFSIZ); |
| 1332 | setvbuf(stderr, NULL, _IONBF, BUFSIZ); |
| 1333 | |
| 1334 | blkid_get_cache(&cache, NULL); |
| 1335 | PRS(argc, argv); |
| 1336 | |
| 1337 | if (!notitle) |
| 1338 | printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); |
| 1339 | |
| 1340 | fstab = getenv("FSTAB_FILE"); |
| 1341 | if (!fstab) |
| 1342 | fstab = _PATH_MNTTAB; |
| 1343 | load_fs_info(fstab); |
| 1344 | |
| 1345 | fsck_path = e2fs_set_sbin_path(); |
| 1346 | |
| 1347 | if ((num_devices == 1) || (serialize)) |
| 1348 | interactive = 1; |
| 1349 | |
| 1350 | /* If -A was specified ("check all"), do that! */ |
| 1351 | if (doall) |
| 1352 | return check_all(); |
| 1353 | |
| 1354 | if (num_devices == 0) { |
| 1355 | serialize++; |
| 1356 | interactive++; |
| 1357 | return check_all(); |
| 1358 | } |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 1359 | for (i = 0; i < num_devices; i++) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1360 | if (cancel_requested) { |
| 1361 | if (!kill_sent) { |
| 1362 | kill_all(SIGTERM); |
| 1363 | kill_sent++; |
| 1364 | } |
| 1365 | break; |
| 1366 | } |
| 1367 | fs = lookup(devices[i]); |
| 1368 | if (!fs) { |
| 1369 | fs = create_fs_device(devices[i], 0, "auto", |
| 1370 | 0, -1, -1); |
| 1371 | if (!fs) |
| 1372 | continue; |
| 1373 | } |
| 1374 | fsck_device(fs, interactive); |
| 1375 | if (serialize || |
| 1376 | (max_running && (num_running >= max_running))) { |
| 1377 | struct fsck_instance *inst; |
| 1378 | |
| 1379 | inst = wait_one(0); |
| 1380 | if (inst) { |
| 1381 | status |= inst->exit_status; |
| 1382 | free_instance(inst); |
| 1383 | } |
| 1384 | if (verbose > 1) |
| 1385 | printf("----------------------------------\n"); |
| 1386 | } |
| 1387 | } |
| 1388 | status |= wait_many(FLAG_WAIT_ALL); |
| 1389 | blkid_put_cache(cache); |
| 1390 | return status; |
| 1391 | } |