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