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