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