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