Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pfsck --- A generic, parallelizing front-end for the fsck program. |
| 3 | * It will automatically try to run fsck programs in parallel if the |
| 4 | * devices are on separate spindles. It is based on the same ideas as |
| 5 | * the generic front end for fsck by David Engel and Fred van Kempen, |
| 6 | * but it has been completely rewritten from scratch to support |
| 7 | * parallel execution. |
| 8 | * |
| 9 | * Written by Theodore Ts'o, <tytso@mit.edu> |
| 10 | * |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 11 | * Usage: fsck [-ACVRNTM] [-s] [-t fstype] [fs-options] device |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | * |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 13 | * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994: |
| 14 | * o Changed -t fstype to behave like with mount when -A (all file |
| 15 | * systems) or -M (like mount) is specified. |
| 16 | * o fsck looks if it can find the fsck.type program to decide |
| 17 | * if it should ignore the fs type. This way more fsck programs |
| 18 | * can be added without changing this front-end. |
| 19 | * o -R flag skip root file system. |
| 20 | * |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 21 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 22 | * |
| 23 | * %Begin-Header% |
| 24 | * This file may be redistributed under the terms of the GNU Public |
| 25 | * License. |
| 26 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 31 | #include <sys/signal.h> |
| 32 | #include <sys/stat.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | #include <limits.h> |
| 34 | #include <stdio.h> |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 35 | #include <ctype.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | #include <string.h> |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 37 | #include <time.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 38 | #if HAVE_STDLIB_H |
| 39 | #include <stdlib.h> |
| 40 | #endif |
| 41 | #if HAVE_ERRNO_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | #include <errno.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 43 | #endif |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 44 | #if HAVE_PATHS_H |
| 45 | #include <paths.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 46 | #endif |
| 47 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 48 | #include <unistd.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 49 | #endif |
| 50 | #if HAVE_ERRNO_H |
| 51 | #include <errno.h> |
| 52 | #endif |
| 53 | #include <malloc.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 54 | |
| 55 | #include "../version.h" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 56 | #include "nls-enable.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | #include "fsck.h" |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 58 | #include "get_device_by_label.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 60 | #ifndef _PATH_MNTTAB |
| 61 | #define _PATH_MNTTAB "/etc/fstab" |
| 62 | #endif |
| 63 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | static const char *ignored_types[] = { |
| 65 | "ignore", |
| 66 | "iso9660", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | "nfs", |
| 68 | "proc", |
| 69 | "sw", |
| 70 | "swap", |
| 71 | NULL |
| 72 | }; |
| 73 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 74 | static const char *really_wanted[] = { |
| 75 | "minix", |
| 76 | "ext2", |
| 77 | "xiafs", |
| 78 | NULL |
| 79 | }; |
| 80 | |
| 81 | #ifdef DEV_DSK_DEVICES |
| 82 | static const char *base_devices[] = { |
| 83 | "/dev/dsk/hda", |
| 84 | "/dev/dsk/hdb", |
| 85 | "/dev/dsk/hdc", |
| 86 | "/dev/dsk/hdd", |
| 87 | "/dev/dsk/hd1a", |
| 88 | "/dev/dsk/hd1b", |
| 89 | "/dev/dsk/hd1c", |
| 90 | "/dev/dsk/hd1d", |
| 91 | "/dev/dsk/sda", |
| 92 | "/dev/dsk/sdb", |
| 93 | "/dev/dsk/sdc", |
| 94 | "/dev/dsk/sdd", |
| 95 | "/dev/dsk/sde", |
| 96 | "/dev/dsk/sdf", |
| 97 | "/dev/dsk/sdg", |
| 98 | NULL |
| 99 | }; |
| 100 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 101 | static const char *base_devices[] = { |
| 102 | "/dev/hda", |
| 103 | "/dev/hdb", |
| 104 | "/dev/hdc", |
| 105 | "/dev/hdd", |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 106 | "/dev/hd1a", |
| 107 | "/dev/hd1b", |
| 108 | "/dev/hd1c", |
| 109 | "/dev/hd1d", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 110 | "/dev/sda", |
| 111 | "/dev/sdb", |
| 112 | "/dev/sdc", |
| 113 | "/dev/sdd", |
| 114 | "/dev/sde", |
| 115 | "/dev/sdf", |
| 116 | "/dev/sdg", |
| 117 | NULL |
| 118 | }; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 119 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Global variables for options |
| 123 | */ |
| 124 | char *devices[MAX_DEVICES]; |
| 125 | char *args[MAX_ARGS]; |
| 126 | int num_devices, num_args; |
| 127 | |
| 128 | int verbose = 0; |
| 129 | int doall = 0; |
| 130 | int noexecute = 0; |
| 131 | int serialize = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 132 | int skip_root = 0; |
| 133 | int like_mount = 0; |
| 134 | int notitle = 0; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 135 | int parallel_root = 0; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 136 | int progress = 0; |
Theodore Ts'o | 3bc0366 | 1999-10-26 17:17:00 +0000 | [diff] [blame] | 137 | int force_all_parallel = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 138 | char *progname; |
| 139 | char *fstype = NULL; |
| 140 | struct fs_info *filesys_info; |
| 141 | struct fsck_instance *instance_list; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 142 | const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc"; |
| 143 | char *fsck_path = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 144 | static int ignore(struct fs_info *); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 145 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 146 | static char *string_copy(const char *s) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 147 | { |
| 148 | char *ret; |
| 149 | |
| 150 | ret = malloc(strlen(s)+1); |
| 151 | if (ret) |
| 152 | strcpy(ret, s); |
| 153 | return ret; |
| 154 | } |
| 155 | |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 156 | static char *skip_over_blank(char *cp) |
| 157 | { |
| 158 | while (*cp && isspace(*cp)) |
| 159 | cp++; |
| 160 | return cp; |
| 161 | } |
| 162 | |
| 163 | static char *skip_over_word(char *cp) |
| 164 | { |
| 165 | while (*cp && !isspace(*cp)) |
| 166 | cp++; |
| 167 | return cp; |
| 168 | } |
| 169 | |
| 170 | static void strip_line(char *line) |
| 171 | { |
| 172 | char *p; |
| 173 | |
| 174 | while (*line) { |
| 175 | p = line + strlen(line) - 1; |
| 176 | if ((*p == '\n') || (*p == '\r')) |
| 177 | *p = 0; |
| 178 | else |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static char *parse_word(char **buf) |
| 184 | { |
| 185 | char *word, *next; |
| 186 | |
| 187 | word = *buf; |
| 188 | if (*word == 0) |
| 189 | return 0; |
| 190 | |
| 191 | word = skip_over_blank(word); |
| 192 | next = skip_over_word(word); |
| 193 | if (*next) |
| 194 | *next++ = 0; |
| 195 | *buf = next; |
| 196 | return word; |
| 197 | } |
| 198 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 199 | static void free_instance(struct fsck_instance *i) |
| 200 | { |
| 201 | if (i->prog) |
| 202 | free(i->prog); |
| 203 | if (i->device) |
| 204 | free(i->device); |
| 205 | free(i); |
| 206 | return; |
| 207 | } |
| 208 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 209 | static int parse_fstab_line(char *line, struct fs_info **ret_fs) |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 210 | { |
| 211 | char *device, *mntpnt, *type, *opts, *freq, *passno, *cp; |
| 212 | struct fs_info *fs; |
| 213 | |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 214 | *ret_fs = 0; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 215 | strip_line(line); |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 216 | if ((cp = strchr(line, '#'))) |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 217 | *cp = 0; /* Ignore everything after the comment char */ |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 218 | cp = line; |
| 219 | |
| 220 | device = parse_word(&cp); |
| 221 | mntpnt = parse_word(&cp); |
| 222 | type = parse_word(&cp); |
| 223 | opts = parse_word(&cp); |
| 224 | freq = parse_word(&cp); |
| 225 | passno = parse_word(&cp); |
| 226 | |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 227 | if (!device) |
| 228 | return 0; /* Allow blank lines */ |
| 229 | |
| 230 | if (!mntpnt || !type) |
| 231 | return -1; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 232 | |
| 233 | if (!(fs = malloc(sizeof(struct fs_info)))) |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 234 | return -1; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 235 | |
| 236 | fs->device = string_copy(device); |
| 237 | fs->mountpt = string_copy(mntpnt); |
| 238 | fs->type = string_copy(type); |
| 239 | fs->opts = string_copy(opts ? opts : ""); |
| 240 | fs->freq = freq ? atoi(freq) : -1; |
| 241 | fs->passno = passno ? atoi(passno) : -1; |
| 242 | fs->flags = 0; |
| 243 | fs->next = NULL; |
| 244 | |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 245 | *ret_fs = fs; |
| 246 | |
| 247 | return 0; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 250 | /* |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 251 | * Interpret the device name if necessary |
| 252 | */ |
| 253 | static char *interpret_device(char *spec) |
| 254 | { |
| 255 | char *dev = NULL; |
| 256 | |
| 257 | if (!strncmp(spec, "UUID=", 5)) |
| 258 | dev = get_spec_by_uuid(spec+5); |
| 259 | else if (!strncmp(spec, "LABEL=", 6)) |
| 260 | dev = get_spec_by_volume_label(spec+6); |
Theodore Ts'o | b4ee1fb | 2000-02-02 19:08:51 +0000 | [diff] [blame] | 261 | else |
| 262 | return spec; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 263 | if (dev) { |
| 264 | free(spec); |
Theodore Ts'o | b4ee1fb | 2000-02-02 19:08:51 +0000 | [diff] [blame] | 265 | return (dev); |
| 266 | } |
| 267 | /* |
| 268 | * Check to see if this was because /proc/partitions isn't |
| 269 | * found. |
| 270 | */ |
| 271 | if (access("/proc/partitions", R_OK) < 0) { |
| 272 | fprintf(stderr, "Couldn't open /proc/partitions: %s\n", |
| 273 | strerror(errno)); |
| 274 | fprintf(stderr, "Is /proc mounted?\n"); |
| 275 | exit(1); |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 276 | } |
Theodore Ts'o | c96e511 | 2000-05-08 14:47:15 +0000 | [diff] [blame] | 277 | /* |
| 278 | * Check to see if this is because we're not running as root |
| 279 | */ |
| 280 | if (geteuid()) |
| 281 | fprintf(stderr, "Must be root to scan for matching " |
| 282 | "filesystems: %s\n", spec); |
| 283 | else |
| 284 | fprintf(stderr, "Couldn't find matching filesystem: %s\n", |
| 285 | spec); |
| 286 | exit(1); |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 290 | * Load the filesystem database from /etc/fstab |
| 291 | */ |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 292 | static void load_fs_info(const char *filename) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 293 | { |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 294 | FILE *f; |
| 295 | char buf[1024]; |
| 296 | int lineno = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 297 | int old_fstab = 1; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 298 | struct fs_info *fs, *fs_last = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 299 | |
| 300 | filesys_info = NULL; |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 301 | if ((f = fopen(filename, "r")) == NULL) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 302 | fprintf(stderr, _("WARNING: couldn't open %s: %s\n"), |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 303 | filename, strerror(errno)); |
| 304 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 305 | } |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 306 | while (!feof(f)) { |
| 307 | lineno++; |
| 308 | if (!fgets(buf, sizeof(buf), f)) |
| 309 | break; |
| 310 | buf[sizeof(buf)-1] = 0; |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 311 | if (parse_fstab_line(buf, &fs) < 0) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 312 | fprintf(stderr, _("WARNING: bad format " |
| 313 | "on line %d of %s\n"), lineno, filename); |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 314 | continue; |
| 315 | } |
Theodore Ts'o | 93ab9d7 | 1999-01-02 04:04:42 +0000 | [diff] [blame] | 316 | if (!fs) |
| 317 | continue; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 318 | fs->device = interpret_device(fs->device); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 319 | if (!filesys_info) |
| 320 | filesys_info = fs; |
| 321 | else |
| 322 | fs_last->next = fs; |
| 323 | fs_last = fs; |
Theodore Ts'o | 665f710 | 1999-01-08 13:33:39 +0000 | [diff] [blame] | 324 | if (fs->passno < 0) |
| 325 | fs->passno = 0; |
| 326 | else |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 327 | old_fstab = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 328 | } |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 329 | |
| 330 | fclose(f); |
| 331 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 332 | if (old_fstab) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 333 | fprintf(stderr, _("\007\007\007" |
| 334 | "WARNING: Your /etc/fstab does not contain the fsck passno\n" |
| 335 | " field. I will kludge around things for you, but you\n" |
| 336 | " should fix your /etc/fstab file as soon as you can.\n\n")); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 337 | |
| 338 | for (fs = filesys_info; fs; fs = fs->next) { |
| 339 | fs->passno = 1; |
| 340 | } |
| 341 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Lookup filesys in /etc/fstab and return the corresponding entry. */ |
| 345 | static struct fs_info *lookup(char *filesys) |
| 346 | { |
| 347 | struct fs_info *fs; |
| 348 | |
| 349 | /* No filesys name given. */ |
| 350 | if (filesys == NULL) |
| 351 | return NULL; |
| 352 | |
| 353 | for (fs = filesys_info; fs; fs = fs->next) { |
| 354 | if (!strcmp(filesys, fs->device) || |
| 355 | !strcmp(filesys, fs->mountpt)) |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | return fs; |
| 360 | } |
| 361 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 362 | /* Find fsck program for a given fs type. */ |
| 363 | static char *find_fsck(char *type) |
| 364 | { |
| 365 | char *s; |
| 366 | const char *tpl; |
| 367 | static char prog[256]; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 368 | char *p = string_copy(fsck_path); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 369 | struct stat st; |
| 370 | |
| 371 | /* Are we looking for a program or just a type? */ |
| 372 | tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s"); |
| 373 | |
| 374 | for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) { |
| 375 | sprintf(prog, tpl, s, type); |
| 376 | if (stat(prog, &st) == 0) break; |
| 377 | } |
| 378 | free(p); |
| 379 | return(s ? prog : NULL); |
| 380 | } |
| 381 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 382 | static int progress_active(NOARGS) |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 383 | { |
| 384 | struct fsck_instance *inst; |
| 385 | |
| 386 | for (inst = instance_list; inst; inst = inst->next) { |
| 387 | if (inst->flags & FLAG_DONE) |
| 388 | continue; |
| 389 | if (inst->flags & FLAG_PROGRESS) |
| 390 | return 1; |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 395 | /* |
| 396 | * Execute a particular fsck program, and link it into the list of |
| 397 | * child processes we are waiting for. |
| 398 | */ |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 399 | static int execute(const char *type, char *device, char *mntpt, |
| 400 | int interactive) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 401 | { |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 402 | char *s, *argv[80], prog[80]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 403 | int argc, i; |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 404 | struct fsck_instance *inst, *p; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 405 | pid_t pid; |
| 406 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 407 | inst = malloc(sizeof(struct fsck_instance)); |
| 408 | if (!inst) |
| 409 | return ENOMEM; |
| 410 | memset(inst, 0, sizeof(struct fsck_instance)); |
| 411 | |
| 412 | sprintf(prog, "fsck.%s", type); |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 413 | argv[0] = string_copy(prog); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 414 | argc = 1; |
| 415 | |
| 416 | for (i=0; i <num_args; i++) |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 417 | argv[argc++] = string_copy(args[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 418 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 419 | if (progress & !progress_active()) { |
| 420 | if (strcmp(type, "ext2") == 0) { |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 421 | argv[argc++] = string_copy("-C0"); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 422 | inst->flags |= FLAG_PROGRESS; |
| 423 | } |
| 424 | } |
| 425 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 426 | argv[argc++] = string_copy(device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 427 | argv[argc] = 0; |
| 428 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 429 | s = find_fsck(prog); |
| 430 | if (s == NULL) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 431 | fprintf(stderr, _("fsck: %s: not found\n"), prog); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 432 | return ENOENT; |
| 433 | } |
| 434 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 435 | if (verbose || noexecute) { |
Theodore Ts'o | 5d45d80 | 1999-03-16 19:35:19 +0000 | [diff] [blame] | 436 | printf("[%s -- %s] ", s, mntpt ? mntpt : device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 437 | for (i=0; i < argc; i++) |
| 438 | printf("%s ", argv[i]); |
| 439 | printf("\n"); |
| 440 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 441 | |
| 442 | /* Fork and execute the correct program. */ |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 443 | if (noexecute) |
| 444 | pid = -1; |
| 445 | else if ((pid = fork()) < 0) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 446 | perror("fork"); |
| 447 | return errno; |
| 448 | } else if (pid == 0) { |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 449 | if (!interactive) |
| 450 | close(0); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 451 | (void) execv(s, argv); |
| 452 | perror(argv[0]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 453 | exit(EXIT_ERROR); |
| 454 | } |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 455 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 456 | for (i=0; i < argc; i++) |
| 457 | free(argv[i]); |
| 458 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 459 | inst->pid = pid; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 460 | inst->prog = string_copy(prog); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 461 | inst->type = string_copy(type); |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 462 | inst->device = string_copy(device); |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 463 | inst->start_time = time(0); |
| 464 | inst->next = NULL; |
| 465 | |
| 466 | /* |
| 467 | * Find the end of the list, so we add the instance on at the end. |
| 468 | */ |
| 469 | for (p = instance_list; p && p->next; p = p->next); |
| 470 | |
| 471 | if (p) |
Theodore Ts'o | ad6783d | 1999-10-26 01:58:54 +0000 | [diff] [blame] | 472 | p->next = inst; |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 473 | else |
| 474 | instance_list = inst; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Wait for one child process to exit; when it does, unlink it from |
| 481 | * the list of executing child processes, and return it. |
| 482 | */ |
| 483 | static struct fsck_instance *wait_one(NOARGS) |
| 484 | { |
| 485 | int status; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 486 | int sig; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 487 | struct fsck_instance *inst, *inst2, *prev; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 488 | pid_t pid; |
| 489 | |
| 490 | if (!instance_list) |
| 491 | return NULL; |
| 492 | |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 493 | if (noexecute) { |
| 494 | inst = instance_list; |
| 495 | instance_list = inst->next; |
| 496 | inst->exit_status = 0; |
| 497 | return(inst); |
| 498 | } |
| 499 | |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 500 | /* |
| 501 | * gcc -Wall fails saving throw against stupidity |
| 502 | * (inst and prev are thought to be uninitialized variables) |
| 503 | */ |
| 504 | inst = prev = NULL; |
| 505 | |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 506 | do { |
| 507 | pid = wait(&status); |
| 508 | if (pid < 0) { |
| 509 | if ((errno == EINTR) || (errno == EAGAIN)) |
| 510 | continue; |
| 511 | if (errno == ECHILD) { |
| 512 | fprintf(stderr, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 513 | _("%s: wait: No more child process?!?\n"), |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 514 | progname); |
| 515 | return NULL; |
| 516 | } |
| 517 | perror("wait"); |
| 518 | continue; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 519 | } |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 520 | for (prev = 0, inst = instance_list; |
| 521 | inst; |
| 522 | prev = inst, inst = inst->next) { |
| 523 | if (inst->pid == pid) |
| 524 | break; |
| 525 | } |
| 526 | } while (!inst); |
| 527 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 528 | if (WIFEXITED(status)) |
| 529 | status = WEXITSTATUS(status); |
| 530 | else if (WIFSIGNALED(status)) { |
| 531 | sig = WTERMSIG(status); |
| 532 | if (sig == SIGINT) { |
| 533 | status = EXIT_UNCORRECTED; |
| 534 | } else { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 535 | printf(_("Warning... %s for device %s exited " |
| 536 | "with signal %d.\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 537 | inst->prog, inst->device, sig); |
| 538 | status = EXIT_ERROR; |
| 539 | } |
| 540 | } else { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 541 | printf(_("%s %s: status is %x, should never happen.\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 542 | inst->prog, inst->device, status); |
| 543 | status = EXIT_ERROR; |
| 544 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 545 | inst->exit_status = status; |
| 546 | if (prev) |
| 547 | prev->next = inst->next; |
| 548 | else |
| 549 | instance_list = inst->next; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 550 | if (progress && (inst->flags & FLAG_PROGRESS) && |
| 551 | !progress_active()) { |
| 552 | for (inst2 = instance_list; inst2; inst2 = inst2->next) { |
| 553 | if (inst2->flags & FLAG_DONE) |
| 554 | continue; |
| 555 | if (strcmp(inst2->type, "ext2")) |
| 556 | continue; |
Theodore Ts'o | 7f4bb6c | 1999-10-20 18:11:01 +0000 | [diff] [blame] | 557 | /* |
| 558 | * If we've just started the fsck, wait a tiny |
| 559 | * bit before sending the kill, to give it |
| 560 | * time to set up the signal handler |
| 561 | */ |
| 562 | if (inst2->start_time < time(0)+2) { |
| 563 | if (fork() == 0) { |
| 564 | sleep(1); |
| 565 | kill(inst2->pid, SIGUSR1); |
| 566 | exit(0); |
| 567 | } |
| 568 | } else |
| 569 | kill(inst2->pid, SIGUSR1); |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 573 | return inst; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Wait until all executing child processes have exited; return the |
| 578 | * logical OR of all of their exit code values. |
| 579 | */ |
| 580 | static int wait_all(NOARGS) |
| 581 | { |
| 582 | struct fsck_instance *inst; |
| 583 | int global_status = 0; |
| 584 | |
| 585 | while (instance_list) { |
| 586 | inst = wait_one(); |
| 587 | if (!inst) |
| 588 | break; |
| 589 | global_status |= inst->exit_status; |
| 590 | free_instance(inst); |
| 591 | } |
| 592 | return global_status; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Run the fsck program on a particular device |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 597 | * |
| 598 | * If the type is specified using -t, and it isn't prefixed with "no" |
| 599 | * (as in "noext2") and only one filesystem type is specified, then |
| 600 | * use that type regardless of what is specified in /etc/fstab. |
| 601 | * |
| 602 | * If the type isn't specified by the user, then use either the type |
| 603 | * specified in /etc/fstab, or DEFAULT_FSTYPE. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 604 | */ |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 605 | static void fsck_device(char *device, int interactive) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 606 | { |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 607 | const char *type = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 608 | struct fs_info *fsent; |
| 609 | int retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 610 | |
Theodore Ts'o | 297f47a | 1997-04-26 14:25:20 +0000 | [diff] [blame] | 611 | if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ',')) |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 612 | type = fstype; |
| 613 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 614 | if ((fsent = lookup(device))) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 615 | device = fsent->device; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 616 | if (!type) |
| 617 | type = fsent->type; |
| 618 | } |
| 619 | if (!type) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 620 | type = DEFAULT_FSTYPE; |
| 621 | |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 622 | retval = execute(type, device, fsent ? fsent->mountpt : 0, |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 623 | interactive); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 624 | if (retval) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 625 | fprintf(stderr, _("%s: Error %d while executing fsck.%s " |
| 626 | "for %s\n"), progname, retval, type, device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 630 | /* See if filesystem type matches the list. */ |
| 631 | static int fs_match(char *type, char *fs_type) |
| 632 | { |
| 633 | int ret = 0, negate = 0; |
| 634 | char list[128]; |
| 635 | char *s; |
| 636 | |
| 637 | if (!fs_type) return(1); |
| 638 | |
| 639 | if (strncmp(fs_type, "no", 2) == 0) { |
| 640 | fs_type += 2; |
| 641 | negate = 1; |
| 642 | } |
| 643 | strcpy(list, fs_type); |
| 644 | s = strtok(list, ","); |
| 645 | while(s) { |
| 646 | if (strcmp(s, type) == 0) { |
| 647 | ret = 1; |
| 648 | break; |
| 649 | } |
| 650 | s = strtok(NULL, ","); |
| 651 | } |
| 652 | return(negate ? !ret : ret); |
| 653 | } |
| 654 | |
| 655 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 656 | /* Check if we should ignore this filesystem. */ |
| 657 | static int ignore(struct fs_info *fs) |
| 658 | { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 659 | const char **ip; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 660 | int wanted = 0; |
| 661 | |
| 662 | /* |
| 663 | * If the pass number is 0, ignore it. |
| 664 | */ |
| 665 | if (fs->passno == 0) |
| 666 | return 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * If a specific fstype is specified, and it doesn't match, |
| 670 | * ignore it. |
| 671 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 672 | if (!fs_match(fs->type, fstype)) return 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 673 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 674 | /* Are we ignoring this type? */ |
| 675 | for(ip = ignored_types; *ip; ip++) |
| 676 | if (strcmp(fs->type, *ip) == 0) return(1); |
| 677 | |
| 678 | /* Do we really really want to check this fs? */ |
| 679 | for(ip = really_wanted; *ip; ip++) |
| 680 | if (strcmp(fs->type, *ip) == 0) { |
| 681 | wanted = 1; |
| 682 | break; |
| 683 | } |
| 684 | |
| 685 | /* See if the <fsck.fs> program is available. */ |
| 686 | if (find_fsck(fs->type) == NULL) { |
| 687 | if (wanted) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 688 | fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 689 | fs->device, fs->type); |
| 690 | return(1); |
| 691 | } |
| 692 | |
| 693 | /* We can and want to check this file system type. */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Return the "base device" given a particular device; this is used to |
| 699 | * assure that we only fsck one partition on a particular drive at any |
| 700 | * one time. Otherwise, the disk heads will be seeking all over the |
| 701 | * place. |
| 702 | */ |
| 703 | static const char *base_device(char *device) |
| 704 | { |
| 705 | const char **base; |
| 706 | |
| 707 | for (base = base_devices; *base; base++) { |
| 708 | if (!strncmp(*base, device, strlen(*base))) |
| 709 | return *base; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 710 | } |
| 711 | return device; |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * Returns TRUE if a partition on the same disk is already being |
| 716 | * checked. |
| 717 | */ |
| 718 | static int device_already_active(char *device) |
| 719 | { |
| 720 | struct fsck_instance *inst; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 721 | const char *base = base_device(device); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 722 | |
Theodore Ts'o | 3bc0366 | 1999-10-26 17:17:00 +0000 | [diff] [blame] | 723 | if (force_all_parallel) |
| 724 | return 0; |
| 725 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 726 | for (inst = instance_list; inst; inst = inst->next) { |
| 727 | if (!strcmp(base, base_device(inst->device))) |
| 728 | return 1; |
| 729 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | /* Check all file systems, using the /etc/fstab table. */ |
| 734 | static int check_all(NOARGS) |
| 735 | { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 736 | struct fs_info *fs = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 737 | struct fsck_instance *inst; |
| 738 | int status = EXIT_OK; |
| 739 | int not_done_yet = 1; |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 740 | int passno = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 741 | int pass_done; |
| 742 | |
| 743 | if (verbose) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 744 | printf(_("Checking all file systems.\n")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * Find and check the root filesystem first. |
| 748 | */ |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 749 | if (!parallel_root) { |
| 750 | for (fs = filesys_info; fs; fs = fs->next) { |
| 751 | if (!strcmp(fs->mountpt, "/")) |
| 752 | break; |
| 753 | } |
| 754 | if (fs && !skip_root && !ignore(fs)) { |
Theodore Ts'o | 3cb7784 | 2000-01-18 16:30:27 +0000 | [diff] [blame] | 755 | fsck_device(fs->device, 1); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 756 | fs->flags |= FLAG_DONE; |
| 757 | status |= wait_all(); |
| 758 | if (status > EXIT_NONDESTRUCT) |
| 759 | return status; |
| 760 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 761 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 762 | if (fs) fs->flags |= FLAG_DONE; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 763 | |
| 764 | /* |
| 765 | * Mark filesystems that should be ignored as done. |
| 766 | */ |
| 767 | for (fs = filesys_info; fs; fs = fs->next) { |
| 768 | if (ignore(fs)) |
| 769 | fs->flags |= FLAG_DONE; |
| 770 | } |
| 771 | |
| 772 | while (not_done_yet) { |
| 773 | not_done_yet = 0; |
| 774 | pass_done = 1; |
| 775 | |
| 776 | for (fs = filesys_info; fs; fs = fs->next) { |
| 777 | if (fs->flags & FLAG_DONE) |
| 778 | continue; |
| 779 | /* |
| 780 | * If the filesystem's pass number is higher |
| 781 | * than the current pass number, then we don't |
| 782 | * do it yet. |
| 783 | */ |
| 784 | if (fs->passno > passno) { |
| 785 | not_done_yet++; |
| 786 | continue; |
| 787 | } |
| 788 | /* |
| 789 | * If a filesystem on a particular device has |
| 790 | * already been spawned, then we need to defer |
| 791 | * this to another pass. |
| 792 | */ |
| 793 | if (device_already_active(fs->device)) { |
| 794 | pass_done = 0; |
| 795 | continue; |
| 796 | } |
| 797 | /* |
| 798 | * Spawn off the fsck process |
| 799 | */ |
Theodore Ts'o | 3cb7784 | 2000-01-18 16:30:27 +0000 | [diff] [blame] | 800 | fsck_device(fs->device, serialize); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 801 | fs->flags |= FLAG_DONE; |
| 802 | |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 803 | if (serialize) { |
| 804 | pass_done = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 805 | break; /* Only do one filesystem at a time */ |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 806 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 807 | } |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 808 | if (verbose > 1) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 809 | printf(_("--waiting-- (pass %d)\n"), passno); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 810 | inst = wait_one(); |
| 811 | if (inst) { |
| 812 | status |= inst->exit_status; |
| 813 | free_instance(inst); |
| 814 | } |
| 815 | if (pass_done) { |
| 816 | status |= wait_all(); |
Theodore Ts'o | ee92299 | 1999-01-16 05:39:12 +0000 | [diff] [blame] | 817 | if (verbose > 1) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 818 | printf("----------------------------------\n"); |
| 819 | passno++; |
| 820 | } else |
| 821 | not_done_yet++; |
| 822 | } |
| 823 | status |= wait_all(); |
| 824 | return status; |
| 825 | } |
| 826 | |
| 827 | static void usage(NOARGS) |
| 828 | { |
| 829 | fprintf(stderr, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 830 | _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 831 | exit(EXIT_USAGE); |
| 832 | } |
| 833 | |
| 834 | static void PRS(int argc, char *argv[]) |
| 835 | { |
| 836 | int i, j; |
| 837 | char *arg; |
| 838 | char options[128]; |
| 839 | int opt = 0; |
| 840 | int opts_for_fsck = 0; |
| 841 | |
| 842 | num_devices = 0; |
| 843 | num_args = 0; |
| 844 | instance_list = 0; |
| 845 | |
| 846 | progname = argv[0]; |
| 847 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 848 | for (i=1; i < argc; i++) { |
| 849 | arg = argv[i]; |
| 850 | if (!arg) |
| 851 | continue; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 852 | if ((arg[0] == '/' && !opts_for_fsck) || |
| 853 | (strncmp(arg, "LABEL=", 6) == 0) || |
| 854 | (strncmp(arg, "UUID=", 5) == 0)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 855 | if (num_devices >= MAX_DEVICES) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 856 | fprintf(stderr, _("%s: too many devices\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 857 | progname); |
| 858 | exit(1); |
| 859 | } |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 860 | devices[num_devices++] = |
| 861 | interpret_device(string_copy(arg)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 862 | continue; |
| 863 | } |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 864 | if (arg[0] != '-' || opts_for_fsck) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 865 | if (num_args >= MAX_ARGS) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 866 | fprintf(stderr, _("%s: too many arguments\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 867 | progname); |
| 868 | exit(1); |
| 869 | } |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 870 | args[num_args++] = string_copy(arg); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 871 | continue; |
| 872 | } |
| 873 | for (j=1; arg[j]; j++) { |
| 874 | if (opts_for_fsck) { |
| 875 | options[++opt] = arg[j]; |
| 876 | continue; |
| 877 | } |
| 878 | switch (arg[j]) { |
| 879 | case 'A': |
| 880 | doall++; |
| 881 | break; |
Theodore Ts'o | 50787ea | 1999-07-19 15:30:21 +0000 | [diff] [blame] | 882 | case 'C': |
| 883 | progress++; |
| 884 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 885 | case 'V': |
| 886 | verbose++; |
| 887 | break; |
| 888 | case 'N': |
| 889 | noexecute++; |
| 890 | break; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 891 | case 'R': |
| 892 | skip_root++; |
| 893 | break; |
| 894 | case 'T': |
| 895 | notitle++; |
| 896 | break; |
| 897 | case 'M': |
| 898 | like_mount++; |
| 899 | break; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 900 | case 'P': |
| 901 | parallel_root++; |
| 902 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 903 | case 's': |
| 904 | serialize++; |
| 905 | break; |
| 906 | case 't': |
| 907 | if (arg[j+1]) { |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 908 | fstype = string_copy(arg+j+1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 909 | goto next_arg; |
| 910 | } |
| 911 | if ((i+1) < argc) { |
| 912 | i++; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 913 | fstype = string_copy(argv[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 914 | goto next_arg; |
| 915 | } |
| 916 | usage(); |
| 917 | break; |
| 918 | case '-': |
| 919 | opts_for_fsck++; |
| 920 | break; |
| 921 | default: |
| 922 | options[++opt] = arg[j]; |
| 923 | break; |
| 924 | } |
| 925 | } |
| 926 | next_arg: |
| 927 | if (opt) { |
| 928 | options[0] = '-'; |
| 929 | options[++opt] = '\0'; |
| 930 | if (num_args >= MAX_ARGS) { |
| 931 | fprintf(stderr, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 932 | _("%s: too many arguments\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 933 | progname); |
| 934 | exit(1); |
| 935 | } |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 936 | args[num_args++] = string_copy(options); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 937 | opt = 0; |
| 938 | } |
| 939 | } |
Theodore Ts'o | 3bc0366 | 1999-10-26 17:17:00 +0000 | [diff] [blame] | 940 | if (getenv("FSCK_FORCE_ALL_PARALLEL")) |
| 941 | force_all_parallel++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | int main(int argc, char *argv[]) |
| 945 | { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 946 | int i; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 947 | int status = 0; |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 948 | int interactive = 0; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 949 | char *oldpath = getenv("PATH"); |
Theodore Ts'o | b513571 | 1999-10-26 14:33:24 +0000 | [diff] [blame] | 950 | const char *fstab; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 951 | |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 952 | #ifdef ENABLE_NLS |
| 953 | setlocale(LC_MESSAGES, ""); |
| 954 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 955 | textdomain(NLS_CAT_NAME); |
| 956 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 957 | PRS(argc, argv); |
| 958 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 959 | if (!notitle) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 960 | printf(_("Parallelizing fsck version %s (%s)\n"), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 961 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 962 | |
Theodore Ts'o | 7d5633c | 1999-02-09 08:14:28 +0000 | [diff] [blame] | 963 | fstab = getenv("FSTAB_FILE"); |
| 964 | if (!fstab) |
| 965 | fstab = _PATH_MNTTAB; |
| 966 | load_fs_info(fstab); |
Theodore Ts'o | 5a679c8 | 1998-12-03 16:40:38 +0000 | [diff] [blame] | 967 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 968 | /* Update our search path to include uncommon directories. */ |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 969 | if (oldpath) { |
| 970 | fsck_path = malloc (strlen (fsck_prefix_path) + 1 + |
| 971 | strlen (oldpath) + 1); |
| 972 | strcpy (fsck_path, fsck_prefix_path); |
| 973 | strcat (fsck_path, ":"); |
| 974 | strcat (fsck_path, oldpath); |
| 975 | } else { |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 976 | fsck_path = string_copy(fsck_prefix_path); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Theodore Ts'o | 6a35ffa | 1999-11-04 22:34:43 +0000 | [diff] [blame] | 979 | if ((num_devices == 1) || (serialize)) |
| 980 | interactive = 1; |
| 981 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 982 | /* If -A was specified ("check all"), do that! */ |
| 983 | if (doall) |
| 984 | return check_all(); |
| 985 | |
| 986 | for (i = 0 ; i < num_devices; i++) { |
Theodore Ts'o | 2d8defd | 1999-07-03 01:59:42 +0000 | [diff] [blame] | 987 | fsck_device(devices[i], interactive); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 988 | if (serialize) { |
| 989 | struct fsck_instance *inst; |
| 990 | |
| 991 | inst = wait_one(); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 992 | if (inst) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 993 | status |= inst->exit_status; |
| 994 | free_instance(inst); |
| 995 | } |
| 996 | } |
| 997 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 998 | status |= wait_all(); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 999 | free(fsck_path); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1000 | return status; |
| 1001 | } |