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