]>
Commit | Line | Data |
---|---|---|
607c2a72 | 1 | /* |
41e32d9f | 2 | * fsck --- A generic, parallelizing front-end for the fsck program. |
607c2a72 KZ |
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 | * | |
11 | * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994: | |
12 | * o Changed -t fstype to behave like with mount when -A (all file | |
13 | * systems) or -M (like mount) is specified. | |
14 | * o fsck looks if it can find the fsck.type program to decide | |
15 | * if it should ignore the fs type. This way more fsck programs | |
16 | * can be added without changing this front-end. | |
17 | * o -R flag skip root file system. | |
18 | * | |
19 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, | |
20 | * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o. | |
21 | * | |
934530c7 | 22 | * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com> |
607c2a72 KZ |
23 | * |
24 | * This file may be redistributed under the terms of the GNU Public | |
25 | * License. | |
26 | */ | |
27 | ||
28 | #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */ | |
29 | ||
30 | #include <sys/types.h> | |
31 | #include <sys/wait.h> | |
607c2a72 | 32 | #include <sys/stat.h> |
dd0bd943 KZ |
33 | #include <sys/file.h> |
34 | #include <fcntl.h> | |
607c2a72 KZ |
35 | #include <limits.h> |
36 | #include <stdio.h> | |
37 | #include <ctype.h> | |
38 | #include <string.h> | |
39 | #include <time.h> | |
40 | #include <stdlib.h> | |
607c2a72 KZ |
41 | #include <paths.h> |
42 | #include <unistd.h> | |
43 | #include <errno.h> | |
607c2a72 | 44 | #include <signal.h> |
0c0f93fc | 45 | #include <dirent.h> |
5a0da00a | 46 | #include <sys/resource.h> |
0556def4 | 47 | #include <sys/time.h> |
0c0f93fc | 48 | #include <blkid.h> |
2b505124 | 49 | #include <libmount.h> |
607c2a72 KZ |
50 | |
51 | #include "nls.h" | |
52 | #include "pathnames.h" | |
947558f5 | 53 | #include "exitcodes.h" |
93bd18bc | 54 | #include "c.h" |
45ca68ec | 55 | #include "closestream.h" |
3bbdae63 | 56 | #include "fileutils.h" |
0a09eb4e | 57 | |
947558f5 | 58 | #define XALLOC_EXIT_CODE FSCK_EX_ERROR |
0a09eb4e | 59 | #include "xalloc.h" |
607c2a72 | 60 | |
7ac166bf KZ |
61 | #ifndef DEFAULT_FSTYPE |
62 | # define DEFAULT_FSTYPE "ext2" | |
63 | #endif | |
64 | ||
65 | #define MAX_DEVICES 32 | |
66 | #define MAX_ARGS 32 | |
67 | ||
3bbdae63 KZ |
68 | #define FSCK_RUNTIME_DIRNAME "/run/fsck" |
69 | ||
607c2a72 KZ |
70 | static const char *ignored_types[] = { |
71 | "ignore", | |
72 | "iso9660", | |
607c2a72 | 73 | "sw", |
607c2a72 KZ |
74 | NULL |
75 | }; | |
76 | ||
77 | static const char *really_wanted[] = { | |
78 | "minix", | |
79 | "ext2", | |
80 | "ext3", | |
81 | "ext4", | |
82 | "ext4dev", | |
83 | "jfs", | |
84 | "reiserfs", | |
c7a96884 | 85 | "xiafs" |
607c2a72 KZ |
86 | }; |
87 | ||
7ac166bf KZ |
88 | /* |
89 | * Internal structure for mount tabel entries. | |
90 | */ | |
91 | struct fsck_fs_data | |
92 | { | |
93 | const char *device; | |
94 | dev_t disk; | |
95 | unsigned int stacked:1, | |
96 | done:1, | |
97 | eval_device:1; | |
98 | }; | |
99 | ||
100 | /* | |
101 | * Structure to allow exit codes to be stored | |
102 | */ | |
103 | struct fsck_instance { | |
104 | int pid; | |
105 | int flags; /* FLAG_{DONE|PROGRESS} */ | |
3bbdae63 KZ |
106 | |
107 | int lock; /* flock()ed lockpath file descriptor or -1 */ | |
108 | char *lockpath; /* /run/fsck/<diskname>.lock or NULL */ | |
109 | ||
7ac166bf | 110 | int exit_status; |
0556def4 KZ |
111 | struct timeval start_time; |
112 | struct timeval end_time; | |
7ac166bf KZ |
113 | char * prog; |
114 | char * type; | |
115 | ||
5a0da00a | 116 | struct rusage rusage; |
7ac166bf KZ |
117 | struct libmnt_fs *fs; |
118 | struct fsck_instance *next; | |
119 | }; | |
120 | ||
121 | #define FLAG_DONE 1 | |
122 | #define FLAG_PROGRESS 2 | |
607c2a72 KZ |
123 | |
124 | /* | |
125 | * Global variables for options | |
126 | */ | |
5894bcd4 KZ |
127 | static char *devices[MAX_DEVICES]; |
128 | static char *args[MAX_ARGS]; | |
129 | static int num_devices, num_args; | |
130 | ||
131 | static int lockdisk; | |
132 | static int verbose; | |
133 | static int doall; | |
134 | static int noexecute; | |
135 | static int serialize; | |
136 | static int skip_root; | |
137 | static int ignore_mounted; | |
138 | static int notitle; | |
139 | static int parallel_root; | |
140 | static int progress; | |
141 | static int progress_fd; | |
142 | static int force_all_parallel; | |
5a0da00a | 143 | static int report_stats; |
5894bcd4 KZ |
144 | |
145 | static int num_running; | |
146 | static int max_running; | |
147 | ||
148 | static volatile int cancel_requested; | |
149 | static int kill_sent; | |
150 | static char *fstype; | |
151 | static struct fsck_instance *instance_list; | |
152 | ||
153 | static const char fsck_prefix_path[] = FS_SEARCH_PATH; | |
154 | static char *fsck_path; | |
155 | ||
156 | /* parsed fstab and mtab */ | |
67f09eae | 157 | static struct libmnt_table *fstab, *mtab; |
e33b39a8 | 158 | static struct libmnt_cache *mntcache; |
2b505124 KZ |
159 | |
160 | static int count_slaves(dev_t disk); | |
161 | ||
607c2a72 KZ |
162 | static int string_to_int(const char *s) |
163 | { | |
164 | long l; | |
165 | char *p; | |
166 | ||
167 | l = strtol(s, &p, 0); | |
168 | if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX) | |
169 | return -1; | |
170 | else | |
171 | return (int) l; | |
172 | } | |
173 | ||
c7a96884 KZ |
174 | /* Do we really really want to check this fs? */ |
175 | static int fs_check_required(const char *type) | |
176 | { | |
177 | size_t i; | |
178 | ||
179 | for(i = 0; i < ARRAY_SIZE(really_wanted); i++) { | |
180 | if (strcmp(type, really_wanted[i]) == 0) | |
181 | return 1; | |
182 | } | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
67f09eae KZ |
187 | static int is_mounted(struct libmnt_fs *fs) |
188 | { | |
189 | int rc; | |
7a4f9885 | 190 | const char *src; |
67f09eae | 191 | |
7a4f9885 KZ |
192 | src = mnt_fs_get_source(fs); |
193 | if (!src) | |
194 | return 0; | |
195 | if (!mntcache) | |
196 | mntcache = mnt_new_cache(); | |
67f09eae KZ |
197 | if (!mtab) { |
198 | mtab = mnt_new_table(); | |
199 | if (!mtab) | |
200 | err(FSCK_EX_ERROR, ("failed to initialize libmount table")); | |
67f09eae KZ |
201 | mnt_table_set_cache(mtab, mntcache); |
202 | mnt_table_parse_mtab(mtab, NULL); | |
203 | } | |
204 | ||
7a4f9885 | 205 | rc = mnt_table_find_source(mtab, src, MNT_ITER_BACKWARD) ? 1 : 0; |
67f09eae KZ |
206 | if (verbose) { |
207 | if (rc) | |
7a4f9885 | 208 | printf(_("%s is mounted\n"), src); |
67f09eae | 209 | else |
7a4f9885 | 210 | printf(_("%s is not mounted\n"), src); |
67f09eae KZ |
211 | } |
212 | return rc; | |
213 | } | |
214 | ||
2b505124 | 215 | static int ignore(struct libmnt_fs *); |
607c2a72 | 216 | |
2b505124 | 217 | static struct fsck_fs_data *fs_create_data(struct libmnt_fs *fs) |
607c2a72 | 218 | { |
2b505124 | 219 | struct fsck_fs_data *data = mnt_fs_get_userdata(fs); |
607c2a72 | 220 | |
2b505124 KZ |
221 | if (!data) { |
222 | data = xcalloc(1, sizeof(*data)); | |
223 | mnt_fs_set_userdata(fs, data); | |
224 | } | |
225 | return data; | |
607c2a72 KZ |
226 | } |
227 | ||
2b505124 KZ |
228 | /* |
229 | * fs from fstab might contains real device name as well as symlink, | |
230 | * LABEL or UUID, this function returns canonicalized result. | |
231 | */ | |
232 | static const char *fs_get_device(struct libmnt_fs *fs) | |
607c2a72 | 233 | { |
2b505124 | 234 | struct fsck_fs_data *data = mnt_fs_get_userdata(fs); |
607c2a72 | 235 | |
2b505124 KZ |
236 | if (!data || !data->eval_device) { |
237 | const char *spec = mnt_fs_get_source(fs); | |
238 | ||
239 | if (!data) | |
240 | data = fs_create_data(fs); | |
241 | ||
242 | data->eval_device = 1; | |
243 | data->device = mnt_resolve_spec(spec, mnt_table_get_cache(fstab)); | |
244 | if (!data->device) | |
2a24e16e | 245 | data->device = xstrdup(spec); |
607c2a72 | 246 | } |
2b505124 KZ |
247 | |
248 | return data->device; | |
607c2a72 KZ |
249 | } |
250 | ||
2b505124 | 251 | static dev_t fs_get_disk(struct libmnt_fs *fs, int check) |
607c2a72 | 252 | { |
2b505124 KZ |
253 | struct fsck_fs_data *data; |
254 | const char *device; | |
255 | struct stat st; | |
256 | ||
257 | data = mnt_fs_get_userdata(fs); | |
258 | if (data && data->disk) | |
259 | return data->disk; | |
607c2a72 | 260 | |
2b505124 | 261 | if (!check) |
607c2a72 KZ |
262 | return 0; |
263 | ||
2b505124 KZ |
264 | if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs)) |
265 | return 0; | |
607c2a72 | 266 | |
2b505124 KZ |
267 | device = fs_get_device(fs); |
268 | if (!device) | |
269 | return 0; | |
607c2a72 | 270 | |
2b505124 | 271 | data = fs_create_data(fs); |
607c2a72 | 272 | |
2b505124 KZ |
273 | if (!stat(device, &st) && |
274 | !blkid_devno_to_wholedisk(st.st_rdev, NULL, 0, &data->disk)) { | |
275 | ||
276 | if (data->disk) | |
277 | data->stacked = count_slaves(data->disk) > 0 ? 1 : 0; | |
278 | return data->disk; | |
607c2a72 | 279 | } |
2b505124 | 280 | return 0; |
607c2a72 KZ |
281 | } |
282 | ||
2b505124 | 283 | static int fs_is_stacked(struct libmnt_fs *fs) |
dd0bd943 | 284 | { |
2b505124 KZ |
285 | struct fsck_fs_data *data = mnt_fs_get_userdata(fs); |
286 | return data ? data->stacked : 0; | |
287 | } | |
dd0bd943 | 288 | |
2b505124 KZ |
289 | static int fs_is_done(struct libmnt_fs *fs) |
290 | { | |
291 | struct fsck_fs_data *data = mnt_fs_get_userdata(fs); | |
292 | return data ? data->done : 0; | |
293 | } | |
dd0bd943 | 294 | |
2b505124 KZ |
295 | static void fs_set_done(struct libmnt_fs *fs) |
296 | { | |
297 | struct fsck_fs_data *data = fs_create_data(fs); | |
298 | ||
299 | if (data) | |
300 | data->done = 1; | |
dd0bd943 KZ |
301 | } |
302 | ||
303 | static int is_irrotational_disk(dev_t disk) | |
304 | { | |
305 | char path[PATH_MAX]; | |
306 | FILE *f; | |
307 | int rc, x; | |
308 | ||
2b505124 | 309 | |
dd0bd943 KZ |
310 | rc = snprintf(path, sizeof(path), |
311 | "/sys/dev/block/%d:%d/queue/rotational", | |
312 | major(disk), minor(disk)); | |
313 | ||
0a09eb4e | 314 | if (rc < 0 || (unsigned int) (rc + 1) > sizeof(path)) |
dd0bd943 KZ |
315 | return 0; |
316 | ||
317 | f = fopen(path, "r"); | |
318 | if (!f) | |
319 | return 0; | |
320 | ||
0a09eb4e SK |
321 | rc = fscanf(f, "%d", &x); |
322 | if (rc != 1) { | |
323 | if (ferror(f)) | |
47481cbd | 324 | warn(_("cannot read %s"), path); |
0a09eb4e SK |
325 | else |
326 | warnx(_("parse error: %s"), path); | |
327 | } | |
dd0bd943 KZ |
328 | fclose(f); |
329 | ||
330 | return rc == 1 ? !x : 0; | |
331 | } | |
332 | ||
333 | static void lock_disk(struct fsck_instance *inst) | |
334 | { | |
2b505124 | 335 | dev_t disk = fs_get_disk(inst->fs, 1); |
3bbdae63 KZ |
336 | char *diskpath = NULL, *diskname; |
337 | ||
338 | inst->lock = -1; | |
dd0bd943 KZ |
339 | |
340 | if (!disk || is_irrotational_disk(disk)) | |
3bbdae63 KZ |
341 | goto done; |
342 | ||
343 | diskpath = blkid_devno_to_devname(disk); | |
344 | if (!diskpath) | |
345 | goto done; | |
346 | ||
347 | if (access(FSCK_RUNTIME_DIRNAME, F_OK) != 0) { | |
348 | int rc = mkdir(FSCK_RUNTIME_DIRNAME, | |
349 | S_IWUSR| | |
350 | S_IRUSR|S_IRGRP|S_IROTH| | |
351 | S_IXUSR|S_IXGRP|S_IXOTH); | |
352 | if (rc && errno != EEXIST) { | |
353 | warn(_("cannot create directory %s"), | |
354 | FSCK_RUNTIME_DIRNAME); | |
355 | goto done; | |
356 | } | |
357 | } | |
dd0bd943 | 358 | |
3bbdae63 | 359 | diskname = stripoff_last_component(diskpath); |
dd0bd943 | 360 | if (!diskname) |
3bbdae63 KZ |
361 | diskname = diskpath; |
362 | ||
363 | xasprintf(&inst->lockpath, FSCK_RUNTIME_DIRNAME "/%s.lock", diskname); | |
dd0bd943 KZ |
364 | |
365 | if (verbose) | |
3bbdae63 | 366 | printf(_("Locking disk by %s ... "), inst->lockpath); |
dd0bd943 | 367 | |
3bbdae63 KZ |
368 | inst->lock = open(inst->lockpath, O_RDONLY|O_CREAT|O_CLOEXEC, |
369 | S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); | |
dd0bd943 KZ |
370 | if (inst->lock >= 0) { |
371 | int rc = -1; | |
372 | ||
373 | /* inform users that we're waiting on the lock */ | |
374 | if (verbose && | |
375 | (rc = flock(inst->lock, LOCK_EX | LOCK_NB)) != 0 && | |
376 | errno == EWOULDBLOCK) | |
377 | printf(_("(waiting) ")); | |
378 | ||
379 | if (rc != 0 && flock(inst->lock, LOCK_EX) != 0) { | |
380 | close(inst->lock); /* failed */ | |
381 | inst->lock = -1; | |
382 | } | |
383 | } | |
384 | ||
385 | if (verbose) | |
fcc058f4 BS |
386 | /* TRANSLATORS: These are followups to "Locking disk...". */ |
387 | printf("%s.\n", inst->lock >= 0 ? _("succeeded") : _("failed")); | |
dd0bd943 | 388 | |
3bbdae63 KZ |
389 | |
390 | done: | |
391 | if (inst->lock < 0) { | |
392 | free(inst->lockpath); | |
393 | inst->lockpath = NULL; | |
394 | } | |
395 | free(diskpath); | |
dd0bd943 KZ |
396 | return; |
397 | } | |
398 | ||
399 | static void unlock_disk(struct fsck_instance *inst) | |
400 | { | |
3bbdae63 KZ |
401 | if (inst->lock < 0) |
402 | return; | |
ac8f2843 | 403 | |
3bbdae63 KZ |
404 | if (verbose) |
405 | printf(_("Unlocking %s.\n"), inst->lockpath); | |
406 | ||
407 | close(inst->lock); /* unlock */ | |
408 | unlink(inst->lockpath); | |
409 | ||
410 | free(inst->lockpath); | |
411 | ||
412 | inst->lock = -1; | |
413 | inst->lockpath = NULL; | |
dd0bd943 KZ |
414 | } |
415 | ||
607c2a72 KZ |
416 | static void free_instance(struct fsck_instance *i) |
417 | { | |
dd0bd943 KZ |
418 | if (lockdisk) |
419 | unlock_disk(i); | |
7009077b | 420 | free(i->prog); |
3bbdae63 | 421 | free(i->lockpath); |
72c08aa9 | 422 | mnt_unref_fs(i->fs); |
607c2a72 KZ |
423 | free(i); |
424 | return; | |
425 | } | |
426 | ||
2b505124 | 427 | static struct libmnt_fs *add_dummy_fs(const char *device) |
607c2a72 | 428 | { |
2b505124 | 429 | struct libmnt_fs *fs = mnt_new_fs(); |
607c2a72 | 430 | |
2b505124 | 431 | if (fs && mnt_fs_set_source(fs, device) == 0 && |
72c08aa9 KZ |
432 | mnt_table_add_fs(fstab, fs) == 0) { |
433 | mnt_unref_fs(fs); | |
2b505124 | 434 | return fs; |
72c08aa9 | 435 | } |
607c2a72 | 436 | |
72c08aa9 | 437 | mnt_unref_fs(fs); |
2b505124 KZ |
438 | err(FSCK_EX_ERROR, _("failed to setup description for %s"), device); |
439 | } | |
607c2a72 | 440 | |
2b505124 | 441 | static void fs_interpret_type(struct libmnt_fs *fs) |
607c2a72 | 442 | { |
2b505124 KZ |
443 | const char *device; |
444 | const char *type = mnt_fs_get_fstype(fs); | |
607c2a72 | 445 | |
2b505124 KZ |
446 | if (type && strcmp(type, "auto") != 0) |
447 | return; | |
607c2a72 | 448 | |
2b505124 | 449 | mnt_fs_set_fstype(fs, NULL); |
607c2a72 | 450 | |
2b505124 KZ |
451 | device = fs_get_device(fs); |
452 | if (device) { | |
453 | int ambi = 0; | |
607c2a72 | 454 | |
2b505124 KZ |
455 | type = mnt_get_fstype(device, &ambi, mnt_table_get_cache(fstab)); |
456 | if (!ambi) | |
457 | mnt_fs_set_fstype(fs, type); | |
458 | } | |
607c2a72 KZ |
459 | } |
460 | ||
2b505124 KZ |
461 | static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)), |
462 | const char *filename, int line) | |
607c2a72 | 463 | { |
2b505124 KZ |
464 | warnx(_("%s: parse error at line %d -- ignore"), filename, line); |
465 | return 0; | |
607c2a72 KZ |
466 | } |
467 | ||
468 | /* | |
469 | * Load the filesystem database from /etc/fstab | |
470 | */ | |
2b505124 | 471 | static void load_fs_info(void) |
607c2a72 | 472 | { |
2b505124 | 473 | const char *path; |
607c2a72 | 474 | |
2b505124 KZ |
475 | fstab = mnt_new_table(); |
476 | if (!fstab) | |
477 | err(FSCK_EX_ERROR, ("failed to initialize libmount table")); | |
607c2a72 | 478 | |
2b505124 | 479 | mnt_table_set_parser_errcb(fstab, parser_errcb); |
e33b39a8 | 480 | mnt_table_set_cache(fstab, mntcache); |
2b505124 KZ |
481 | |
482 | errno = 0; | |
483 | ||
484 | /* | |
485 | * Let's follow libmount defauls if $FSTAB_FILE is not specified | |
486 | */ | |
487 | path = getenv("FSTAB_FILE"); | |
488 | ||
489 | if (mnt_table_parse_fstab(fstab, path)) { | |
2b505124 KZ |
490 | if (!path) |
491 | path = mnt_get_fstab_path(); | |
0525768a KZ |
492 | |
493 | /* don't print error when there is no fstab at all */ | |
494 | if (access(path, F_OK) == 0) { | |
495 | if (errno) | |
496 | warn(_("%s: failed to parse fstab"), path); | |
497 | else | |
498 | warnx(_("%s: failed to parse fstab"), path); | |
499 | } | |
607c2a72 KZ |
500 | } |
501 | } | |
502 | ||
e33b39a8 KZ |
503 | /* |
504 | * Lookup filesys in /etc/fstab and return the corresponding entry. | |
505 | * The @path has to be real path (no TAG) by mnt_resolve_spec(). | |
506 | */ | |
507 | static struct libmnt_fs *lookup(char *path) | |
607c2a72 | 508 | { |
2b505124 | 509 | struct libmnt_fs *fs; |
607c2a72 | 510 | |
e33b39a8 | 511 | if (!path) |
607c2a72 KZ |
512 | return NULL; |
513 | ||
e33b39a8 | 514 | fs = mnt_table_find_srcpath(fstab, path, MNT_ITER_FORWARD); |
7a4f9885 KZ |
515 | if (!fs) { |
516 | /* | |
517 | * Maybe mountpoint has been specified on fsck command line. | |
518 | * Yeah, crazy feature... | |
519 | * | |
520 | * Note that mnt_table_find_target() may canonicalize paths in | |
521 | * the fstab to support symlinks. This is really unwanted, | |
522 | * because readlink() on mountpoints may trigger automounts. | |
523 | * | |
524 | * So, disable the cache and compare the paths as strings | |
525 | * without care about symlinks... | |
526 | */ | |
527 | mnt_table_set_cache(fstab, NULL); | |
e33b39a8 | 528 | fs = mnt_table_find_target(fstab, path, MNT_ITER_FORWARD); |
7a4f9885 KZ |
529 | mnt_table_set_cache(fstab, mntcache); |
530 | } | |
607c2a72 KZ |
531 | return fs; |
532 | } | |
533 | ||
534 | /* Find fsck program for a given fs type. */ | |
2b505124 | 535 | static char *find_fsck(const char *type) |
607c2a72 | 536 | { |
41e32d9f KZ |
537 | char *s; |
538 | const char *tpl; | |
539 | static char prog[256]; | |
2a24e16e | 540 | char *p = xstrdup(fsck_path); |
41e32d9f KZ |
541 | struct stat st; |
542 | ||
543 | /* Are we looking for a program or just a type? */ | |
544 | tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s"); | |
545 | ||
546 | for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) { | |
547 | sprintf(prog, tpl, s, type); | |
7064679f KZ |
548 | if (stat(prog, &st) == 0) |
549 | break; | |
41e32d9f KZ |
550 | } |
551 | free(p); | |
552 | ||
553 | return(s ? prog : NULL); | |
607c2a72 KZ |
554 | } |
555 | ||
7ac166bf | 556 | static int progress_active(void) |
607c2a72 KZ |
557 | { |
558 | struct fsck_instance *inst; | |
559 | ||
560 | for (inst = instance_list; inst; inst = inst->next) { | |
561 | if (inst->flags & FLAG_DONE) | |
562 | continue; | |
563 | if (inst->flags & FLAG_PROGRESS) | |
564 | return 1; | |
565 | } | |
566 | return 0; | |
567 | } | |
568 | ||
5a0da00a FM |
569 | /* |
570 | * Process run statistics for finished fsck instances. | |
571 | * | |
572 | * If report_stats is 0, do nothing, otherwise print a selection of | |
573 | * interesting rusage statistics as well as elapsed wallclock time. | |
574 | */ | |
575 | static void print_stats(struct fsck_instance *inst) | |
576 | { | |
0556def4 | 577 | double time_diff; |
5a0da00a FM |
578 | |
579 | if (!inst || !report_stats || noexecute) | |
580 | return; | |
581 | ||
0556def4 KZ |
582 | time_diff = (inst->end_time.tv_sec - inst->start_time.tv_sec) |
583 | + (inst->end_time.tv_usec - inst->start_time.tv_usec) / 1E6; | |
584 | ||
585 | fprintf(stdout, "%s: status %d, rss %ld, " | |
586 | "real %f, user %d.%06d, sys %d.%06d\n", | |
5a0da00a FM |
587 | fs_get_device(inst->fs), |
588 | inst->exit_status, | |
589 | inst->rusage.ru_maxrss, | |
0556def4 | 590 | time_diff, |
5a0da00a FM |
591 | (int)inst->rusage.ru_utime.tv_sec, |
592 | (int)inst->rusage.ru_utime.tv_usec, | |
593 | (int)inst->rusage.ru_stime.tv_sec, | |
594 | (int)inst->rusage.ru_stime.tv_usec); | |
595 | } | |
596 | ||
607c2a72 KZ |
597 | /* |
598 | * Execute a particular fsck program, and link it into the list of | |
599 | * child processes we are waiting for. | |
600 | */ | |
c7a96884 KZ |
601 | static int execute(const char *progname, const char *progpath, |
602 | const char *type, struct libmnt_fs *fs, int interactive) | |
607c2a72 | 603 | { |
c7a96884 | 604 | char *argv[80]; |
607c2a72 KZ |
605 | int argc, i; |
606 | struct fsck_instance *inst, *p; | |
607 | pid_t pid; | |
608 | ||
ac8f2843 | 609 | inst = xcalloc(1, sizeof(*inst)); |
607c2a72 | 610 | |
c7a96884 | 611 | argv[0] = xstrdup(progname); |
607c2a72 KZ |
612 | argc = 1; |
613 | ||
614 | for (i=0; i <num_args; i++) | |
2a24e16e | 615 | argv[argc++] = xstrdup(args[i]); |
607c2a72 KZ |
616 | |
617 | if (progress) { | |
618 | if ((strcmp(type, "ext2") == 0) || | |
619 | (strcmp(type, "ext3") == 0) || | |
620 | (strcmp(type, "ext4") == 0) || | |
621 | (strcmp(type, "ext4dev") == 0)) { | |
622 | char tmp[80]; | |
623 | ||
624 | tmp[0] = 0; | |
625 | if (!progress_active()) { | |
626 | snprintf(tmp, 80, "-C%d", progress_fd); | |
627 | inst->flags |= FLAG_PROGRESS; | |
628 | } else if (progress_fd) | |
629 | snprintf(tmp, 80, "-C%d", progress_fd * -1); | |
630 | if (tmp[0]) | |
2a24e16e | 631 | argv[argc++] = xstrdup(tmp); |
607c2a72 KZ |
632 | } |
633 | } | |
634 | ||
2a24e16e | 635 | argv[argc++] = xstrdup(fs_get_device(fs)); |
607c2a72 KZ |
636 | argv[argc] = 0; |
637 | ||
607c2a72 | 638 | if (verbose || noexecute) { |
2b505124 KZ |
639 | const char *tgt = mnt_fs_get_target(fs); |
640 | ||
641 | if (!tgt) | |
642 | tgt = fs_get_device(fs); | |
c7a96884 | 643 | printf("[%s (%d) -- %s] ", progpath, num_running, tgt); |
607c2a72 KZ |
644 | for (i=0; i < argc; i++) |
645 | printf("%s ", argv[i]); | |
646 | printf("\n"); | |
647 | } | |
648 | ||
72c08aa9 | 649 | mnt_ref_fs(fs); |
dd0bd943 KZ |
650 | inst->fs = fs; |
651 | inst->lock = -1; | |
652 | ||
653 | if (lockdisk) | |
654 | lock_disk(inst); | |
655 | ||
607c2a72 KZ |
656 | /* Fork and execute the correct program. */ |
657 | if (noexecute) | |
658 | pid = -1; | |
659 | else if ((pid = fork()) < 0) { | |
ac8f2843 | 660 | warn(_("fork failed")); |
72c08aa9 | 661 | free_instance(inst); |
607c2a72 KZ |
662 | return errno; |
663 | } else if (pid == 0) { | |
664 | if (!interactive) | |
665 | close(0); | |
c7a96884 KZ |
666 | execv(progpath, argv); |
667 | err(FSCK_EX_ERROR, _("%s: execute failed"), progpath); | |
607c2a72 KZ |
668 | } |
669 | ||
670 | for (i=0; i < argc; i++) | |
671 | free(argv[i]); | |
672 | ||
673 | inst->pid = pid; | |
c7a96884 | 674 | inst->prog = xstrdup(progname); |
2a24e16e | 675 | inst->type = xstrdup(type); |
0556def4 | 676 | gettimeofday(&inst->start_time, NULL); |
607c2a72 KZ |
677 | inst->next = NULL; |
678 | ||
679 | /* | |
680 | * Find the end of the list, so we add the instance on at the end. | |
681 | */ | |
682 | for (p = instance_list; p && p->next; p = p->next); | |
683 | ||
684 | if (p) | |
685 | p->next = inst; | |
686 | else | |
687 | instance_list = inst; | |
688 | ||
689 | return 0; | |
690 | } | |
691 | ||
692 | /* | |
693 | * Send a signal to all outstanding fsck child processes | |
694 | */ | |
695 | static int kill_all(int signum) | |
696 | { | |
697 | struct fsck_instance *inst; | |
698 | int n = 0; | |
699 | ||
700 | for (inst = instance_list; inst; inst = inst->next) { | |
701 | if (inst->flags & FLAG_DONE) | |
702 | continue; | |
703 | kill(inst->pid, signum); | |
704 | n++; | |
705 | } | |
706 | return n; | |
707 | } | |
708 | ||
709 | /* | |
710 | * Wait for one child process to exit; when it does, unlink it from | |
711 | * the list of executing child processes, and return it. | |
712 | */ | |
713 | static struct fsck_instance *wait_one(int flags) | |
714 | { | |
1bf9e264 | 715 | int status = 0; |
607c2a72 KZ |
716 | int sig; |
717 | struct fsck_instance *inst, *inst2, *prev; | |
718 | pid_t pid; | |
5a0da00a | 719 | struct rusage rusage; |
607c2a72 KZ |
720 | |
721 | if (!instance_list) | |
722 | return NULL; | |
723 | ||
724 | if (noexecute) { | |
725 | inst = instance_list; | |
726 | prev = 0; | |
727 | #ifdef RANDOM_DEBUG | |
728 | while (inst->next && (random() & 1)) { | |
729 | prev = inst; | |
730 | inst = inst->next; | |
731 | } | |
732 | #endif | |
733 | inst->exit_status = 0; | |
734 | goto ret_inst; | |
735 | } | |
736 | ||
737 | /* | |
738 | * gcc -Wall fails saving throw against stupidity | |
739 | * (inst and prev are thought to be uninitialized variables) | |
740 | */ | |
741 | inst = prev = NULL; | |
742 | ||
743 | do { | |
5a0da00a | 744 | pid = wait4(-1, &status, flags, &rusage); |
607c2a72 KZ |
745 | if (cancel_requested && !kill_sent) { |
746 | kill_all(SIGTERM); | |
747 | kill_sent++; | |
748 | } | |
749 | if ((pid == 0) && (flags & WNOHANG)) | |
750 | return NULL; | |
751 | if (pid < 0) { | |
752 | if ((errno == EINTR) || (errno == EAGAIN)) | |
753 | continue; | |
754 | if (errno == ECHILD) { | |
0a09eb4e | 755 | warnx(_("wait: no more child process?!?")); |
607c2a72 KZ |
756 | return NULL; |
757 | } | |
83217641 | 758 | warn(_("waitpid failed")); |
607c2a72 KZ |
759 | continue; |
760 | } | |
761 | for (prev = 0, inst = instance_list; | |
762 | inst; | |
763 | prev = inst, inst = inst->next) { | |
764 | if (inst->pid == pid) | |
765 | break; | |
766 | } | |
767 | } while (!inst); | |
768 | ||
769 | if (WIFEXITED(status)) | |
770 | status = WEXITSTATUS(status); | |
771 | else if (WIFSIGNALED(status)) { | |
772 | sig = WTERMSIG(status); | |
773 | if (sig == SIGINT) { | |
947558f5 | 774 | status = FSCK_EX_UNCORRECTED; |
607c2a72 | 775 | } else { |
0a09eb4e SK |
776 | warnx(_("Warning... %s for device %s exited " |
777 | "with signal %d."), | |
2b505124 | 778 | inst->prog, fs_get_device(inst->fs), sig); |
947558f5 | 779 | status = FSCK_EX_ERROR; |
607c2a72 KZ |
780 | } |
781 | } else { | |
0a09eb4e | 782 | warnx(_("%s %s: status is %x, should never happen."), |
2b505124 | 783 | inst->prog, fs_get_device(inst->fs), status); |
947558f5 | 784 | status = FSCK_EX_ERROR; |
607c2a72 | 785 | } |
5a0da00a | 786 | |
607c2a72 KZ |
787 | inst->exit_status = status; |
788 | inst->flags |= FLAG_DONE; | |
0556def4 | 789 | gettimeofday(&inst->end_time, NULL); |
5a0da00a FM |
790 | memcpy(&inst->rusage, &rusage, sizeof(struct rusage)); |
791 | ||
607c2a72 KZ |
792 | if (progress && (inst->flags & FLAG_PROGRESS) && |
793 | !progress_active()) { | |
794 | for (inst2 = instance_list; inst2; inst2 = inst2->next) { | |
795 | if (inst2->flags & FLAG_DONE) | |
796 | continue; | |
797 | if (strcmp(inst2->type, "ext2") && | |
798 | strcmp(inst2->type, "ext3") && | |
799 | strcmp(inst2->type, "ext4") && | |
800 | strcmp(inst2->type, "ext4dev")) | |
801 | continue; | |
802 | /* | |
803 | * If we've just started the fsck, wait a tiny | |
804 | * bit before sending the kill, to give it | |
805 | * time to set up the signal handler | |
806 | */ | |
0556def4 | 807 | if (inst2->start_time.tv_sec < time(0) + 2) { |
607c2a72 KZ |
808 | if (fork() == 0) { |
809 | sleep(1); | |
810 | kill(inst2->pid, SIGUSR1); | |
947558f5 | 811 | exit(FSCK_EX_OK); |
607c2a72 KZ |
812 | } |
813 | } else | |
814 | kill(inst2->pid, SIGUSR1); | |
815 | inst2->flags |= FLAG_PROGRESS; | |
816 | break; | |
817 | } | |
818 | } | |
819 | ret_inst: | |
820 | if (prev) | |
821 | prev->next = inst->next; | |
822 | else | |
823 | instance_list = inst->next; | |
5a0da00a FM |
824 | |
825 | print_stats(inst); | |
826 | ||
607c2a72 KZ |
827 | if (verbose > 1) |
828 | printf(_("Finished with %s (exit status %d)\n"), | |
2b505124 | 829 | fs_get_device(inst->fs), inst->exit_status); |
607c2a72 KZ |
830 | num_running--; |
831 | return inst; | |
832 | } | |
833 | ||
834 | #define FLAG_WAIT_ALL 0 | |
835 | #define FLAG_WAIT_ATLEAST_ONE 1 | |
836 | /* | |
837 | * Wait until all executing child processes have exited; return the | |
838 | * logical OR of all of their exit code values. | |
839 | */ | |
840 | static int wait_many(int flags) | |
841 | { | |
842 | struct fsck_instance *inst; | |
843 | int global_status = 0; | |
844 | int wait_flags = 0; | |
845 | ||
846 | while ((inst = wait_one(wait_flags))) { | |
847 | global_status |= inst->exit_status; | |
848 | free_instance(inst); | |
849 | #ifdef RANDOM_DEBUG | |
850 | if (noexecute && (flags & WNOHANG) && !(random() % 3)) | |
851 | break; | |
852 | #endif | |
853 | if (flags & FLAG_WAIT_ATLEAST_ONE) | |
854 | wait_flags = WNOHANG; | |
855 | } | |
856 | return global_status; | |
857 | } | |
858 | ||
859 | /* | |
860 | * Run the fsck program on a particular device | |
861 | * | |
862 | * If the type is specified using -t, and it isn't prefixed with "no" | |
863 | * (as in "noext2") and only one filesystem type is specified, then | |
864 | * use that type regardless of what is specified in /etc/fstab. | |
865 | * | |
866 | * If the type isn't specified by the user, then use either the type | |
867 | * specified in /etc/fstab, or DEFAULT_FSTYPE. | |
868 | */ | |
2b505124 | 869 | static int fsck_device(struct libmnt_fs *fs, int interactive) |
607c2a72 | 870 | { |
c7a96884 | 871 | char progname[80], *progpath; |
607c2a72 KZ |
872 | const char *type; |
873 | int retval; | |
874 | ||
2b505124 KZ |
875 | fs_interpret_type(fs); |
876 | ||
877 | type = mnt_fs_get_fstype(fs); | |
607c2a72 | 878 | |
2b505124 KZ |
879 | if (type && strcmp(type, "auto") != 0) |
880 | ; | |
607c2a72 KZ |
881 | else if (fstype && strncmp(fstype, "no", 2) && |
882 | strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) && | |
883 | !strchr(fstype, ',')) | |
884 | type = fstype; | |
885 | else | |
886 | type = DEFAULT_FSTYPE; | |
887 | ||
c7a96884 KZ |
888 | sprintf(progname, "fsck.%s", type); |
889 | progpath = find_fsck(progname); | |
890 | if (progpath == NULL) { | |
891 | if (fs_check_required(type)) { | |
892 | retval = ENOENT; | |
893 | goto err; | |
894 | } | |
895 | return 0; | |
896 | } | |
897 | ||
607c2a72 | 898 | num_running++; |
c7a96884 | 899 | retval = execute(progname, progpath, type, fs, interactive); |
607c2a72 | 900 | if (retval) { |
607c2a72 | 901 | num_running--; |
c7a96884 | 902 | goto err; |
607c2a72 | 903 | } |
6c6f2af9 | 904 | return 0; |
c7a96884 KZ |
905 | err: |
906 | warnx(_("error %d (%m) while executing fsck.%s for %s"), | |
907 | retval, type, fs_get_device(fs)); | |
908 | return FSCK_EX_ERROR; | |
607c2a72 KZ |
909 | } |
910 | ||
911 | ||
912 | /* | |
913 | * Deal with the fsck -t argument. | |
914 | */ | |
915 | struct fs_type_compile { | |
916 | char **list; | |
917 | int *type; | |
918 | int negate; | |
919 | } fs_type_compiled; | |
920 | ||
921 | #define FS_TYPE_NORMAL 0 | |
922 | #define FS_TYPE_OPT 1 | |
923 | #define FS_TYPE_NEGOPT 2 | |
924 | ||
607c2a72 KZ |
925 | static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp) |
926 | { | |
5894bcd4 | 927 | char *cp, *list, *s; |
607c2a72 KZ |
928 | int num = 2; |
929 | int negate, first_negate = 1; | |
930 | ||
931 | if (fs_type) { | |
932 | for (cp=fs_type; *cp; cp++) { | |
933 | if (*cp == ',') | |
934 | num++; | |
935 | } | |
936 | } | |
937 | ||
ac8f2843 KZ |
938 | cmp->list = xcalloc(num, sizeof(char *)); |
939 | cmp->type = xcalloc(num, sizeof(int)); | |
607c2a72 KZ |
940 | cmp->negate = 0; |
941 | ||
942 | if (!fs_type) | |
943 | return; | |
944 | ||
2a24e16e | 945 | list = xstrdup(fs_type); |
607c2a72 KZ |
946 | num = 0; |
947 | s = strtok(list, ","); | |
948 | while(s) { | |
949 | negate = 0; | |
950 | if (strncmp(s, "no", 2) == 0) { | |
951 | s += 2; | |
952 | negate = 1; | |
953 | } else if (*s == '!') { | |
954 | s++; | |
955 | negate = 1; | |
956 | } | |
957 | if (strcmp(s, "loop") == 0) | |
958 | /* loop is really short-hand for opts=loop */ | |
959 | goto loop_special_case; | |
960 | else if (strncmp(s, "opts=", 5) == 0) { | |
961 | s += 5; | |
962 | loop_special_case: | |
963 | cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT; | |
964 | } else { | |
965 | if (first_negate) { | |
966 | cmp->negate = negate; | |
967 | first_negate = 0; | |
968 | } | |
969 | if ((negate && !cmp->negate) || | |
970 | (!negate && cmp->negate)) { | |
947558f5 | 971 | errx(FSCK_EX_USAGE, |
e0eadc0d SK |
972 | _("Either all or none of the filesystem types passed to -t must be prefixed\n" |
973 | "with 'no' or '!'.")); | |
607c2a72 KZ |
974 | } |
975 | } | |
ac8f2843 | 976 | |
2a24e16e | 977 | cmp->list[num++] = xstrdup(s); |
607c2a72 KZ |
978 | s = strtok(NULL, ","); |
979 | } | |
980 | free(list); | |
981 | } | |
982 | ||
983 | /* | |
984 | * This function returns true if a particular option appears in a | |
985 | * comma-delimited options list | |
986 | */ | |
2b505124 | 987 | static int opt_in_list(const char *opt, const char *optlist) |
607c2a72 KZ |
988 | { |
989 | char *list, *s; | |
990 | ||
991 | if (!optlist) | |
992 | return 0; | |
2a24e16e | 993 | list = xstrdup(optlist); |
607c2a72 KZ |
994 | |
995 | s = strtok(list, ","); | |
996 | while(s) { | |
997 | if (strcmp(s, opt) == 0) { | |
998 | free(list); | |
999 | return 1; | |
1000 | } | |
1001 | s = strtok(NULL, ","); | |
1002 | } | |
0a09eb4e | 1003 | free(list); |
607c2a72 KZ |
1004 | return 0; |
1005 | } | |
1006 | ||
1007 | /* See if the filesystem matches the criteria given by the -t option */ | |
2b505124 | 1008 | static int fs_match(struct libmnt_fs *fs, struct fs_type_compile *cmp) |
607c2a72 KZ |
1009 | { |
1010 | int n, ret = 0, checked_type = 0; | |
1011 | char *cp; | |
1012 | ||
1013 | if (cmp->list == 0 || cmp->list[0] == 0) | |
1014 | return 1; | |
1015 | ||
1016 | for (n=0; (cp = cmp->list[n]); n++) { | |
1017 | switch (cmp->type[n]) { | |
1018 | case FS_TYPE_NORMAL: | |
2b505124 KZ |
1019 | { |
1020 | const char *type = mnt_fs_get_fstype(fs); | |
1021 | ||
607c2a72 | 1022 | checked_type++; |
2b505124 | 1023 | if (type && strcmp(cp, type) == 0) |
607c2a72 | 1024 | ret = 1; |
607c2a72 | 1025 | break; |
2b505124 | 1026 | } |
607c2a72 | 1027 | case FS_TYPE_NEGOPT: |
2b505124 | 1028 | if (opt_in_list(cp, mnt_fs_get_options(fs))) |
607c2a72 KZ |
1029 | return 0; |
1030 | break; | |
1031 | case FS_TYPE_OPT: | |
2b505124 | 1032 | if (!opt_in_list(cp, mnt_fs_get_options(fs))) |
607c2a72 KZ |
1033 | return 0; |
1034 | break; | |
1035 | } | |
1036 | } | |
1037 | if (checked_type == 0) | |
1038 | return 1; | |
1039 | return (cmp->negate ? !ret : ret); | |
1040 | } | |
1041 | ||
1bb516c3 LN |
1042 | /* |
1043 | * Check if a device exists | |
1044 | */ | |
1045 | static int device_exists(const char *device) | |
1046 | { | |
1047 | struct stat st; | |
1048 | ||
1049 | if (stat(device, &st) == -1) | |
1050 | return 0; | |
1bb516c3 LN |
1051 | if (!S_ISBLK(st.st_mode)) |
1052 | return 0; | |
1bb516c3 LN |
1053 | return 1; |
1054 | } | |
1055 | ||
2b505124 | 1056 | static int fs_ignored_type(struct libmnt_fs *fs) |
e3174198 | 1057 | { |
2b505124 KZ |
1058 | const char **ip, *type; |
1059 | ||
1060 | if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs) || mnt_fs_is_swaparea(fs)) | |
1061 | return 1; | |
1062 | ||
1063 | type = mnt_fs_get_fstype(fs); | |
e3174198 | 1064 | |
2b505124 KZ |
1065 | for(ip = ignored_types; type && *ip; ip++) { |
1066 | if (strcmp(type, *ip) == 0) | |
e3174198 KZ |
1067 | return 1; |
1068 | } | |
1069 | ||
1070 | return 0; | |
1071 | } | |
1072 | ||
607c2a72 | 1073 | /* Check if we should ignore this filesystem. */ |
2b505124 | 1074 | static int ignore(struct libmnt_fs *fs) |
607c2a72 | 1075 | { |
c7a96884 | 1076 | const char *type; |
607c2a72 KZ |
1077 | |
1078 | /* | |
1079 | * If the pass number is 0, ignore it. | |
1080 | */ | |
2b505124 | 1081 | if (mnt_fs_get_passno(fs) == 0) |
607c2a72 KZ |
1082 | return 1; |
1083 | ||
1084 | /* | |
1085 | * If this is a bind mount, ignore it. | |
1086 | */ | |
2b505124 | 1087 | if (opt_in_list("bind", mnt_fs_get_options(fs))) { |
0a09eb4e SK |
1088 | warnx(_("%s: skipping bad line in /etc/fstab: " |
1089 | "bind mount with nonzero fsck pass number"), | |
2b505124 | 1090 | mnt_fs_get_target(fs)); |
607c2a72 KZ |
1091 | return 1; |
1092 | } | |
1093 | ||
1bb516c3 LN |
1094 | /* |
1095 | * ignore devices that don't exist and have the "nofail" mount option | |
1096 | */ | |
2b505124 KZ |
1097 | if (!device_exists(fs_get_device(fs))) { |
1098 | if (opt_in_list("nofail", mnt_fs_get_options(fs))) { | |
236acf2d KZ |
1099 | if (verbose) |
1100 | printf(_("%s: skipping nonexistent device\n"), | |
2b505124 | 1101 | fs_get_device(fs)); |
236acf2d KZ |
1102 | return 1; |
1103 | } | |
1bb516c3 | 1104 | if (verbose) |
236acf2d KZ |
1105 | printf(_("%s: nonexistent device (\"nofail\" fstab " |
1106 | "option may be used to skip this device)\n"), | |
2b505124 | 1107 | fs_get_device(fs)); |
1bb516c3 LN |
1108 | } |
1109 | ||
2b505124 | 1110 | fs_interpret_type(fs); |
607c2a72 KZ |
1111 | |
1112 | /* | |
1113 | * If a specific fstype is specified, and it doesn't match, | |
1114 | * ignore it. | |
1115 | */ | |
3d4820f4 KZ |
1116 | if (!fs_match(fs, &fs_type_compiled)) |
1117 | return 1; | |
607c2a72 | 1118 | |
2b505124 | 1119 | type = mnt_fs_get_fstype(fs); |
7064679f KZ |
1120 | if (!type) { |
1121 | if (verbose) | |
1122 | printf(_("%s: skipping unknown filesystem type\n"), | |
1123 | fs_get_device(fs)); | |
1124 | return 1; | |
1125 | } | |
2b505124 | 1126 | |
607c2a72 | 1127 | /* Are we ignoring this type? */ |
2b505124 | 1128 | if (fs_ignored_type(fs)) |
e3174198 | 1129 | return 1; |
607c2a72 | 1130 | |
c7a96884 | 1131 | |
607c2a72 KZ |
1132 | |
1133 | /* See if the <fsck.fs> program is available. */ | |
2b505124 | 1134 | if (find_fsck(type) == NULL) { |
c7a96884 | 1135 | if (fs_check_required(type)) |
0a09eb4e | 1136 | warnx(_("cannot check %s: fsck.%s not found"), |
2b505124 | 1137 | fs_get_device(fs), type); |
607c2a72 KZ |
1138 | return 1; |
1139 | } | |
1140 | ||
1141 | /* We can and want to check this file system type. */ | |
1142 | return 0; | |
1143 | } | |
1144 | ||
0c0f93fc KZ |
1145 | static int count_slaves(dev_t disk) |
1146 | { | |
1147 | DIR *dir; | |
1148 | struct dirent *dp; | |
1149 | char dirname[PATH_MAX]; | |
1150 | int count = 0; | |
1151 | ||
1152 | snprintf(dirname, sizeof(dirname), | |
1153 | "/sys/dev/block/%u:%u/slaves/", | |
1154 | major(disk), minor(disk)); | |
1155 | ||
1156 | if (!(dir = opendir(dirname))) | |
1157 | return -1; | |
1158 | ||
1159 | while ((dp = readdir(dir)) != 0) { | |
1160 | #ifdef _DIRENT_HAVE_D_TYPE | |
1161 | if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK) | |
1162 | continue; | |
1163 | #endif | |
1164 | if (dp->d_name[0] == '.' && | |
1165 | ((dp->d_name[1] == 0) || | |
1166 | ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) | |
1167 | continue; | |
1168 | ||
1169 | count++; | |
1170 | } | |
ac8f2843 | 1171 | |
0c0f93fc KZ |
1172 | closedir(dir); |
1173 | return count; | |
1174 | } | |
1175 | ||
607c2a72 KZ |
1176 | /* |
1177 | * Returns TRUE if a partition on the same disk is already being | |
1178 | * checked. | |
1179 | */ | |
2b505124 | 1180 | static int disk_already_active(struct libmnt_fs *fs) |
607c2a72 KZ |
1181 | { |
1182 | struct fsck_instance *inst; | |
2b505124 | 1183 | dev_t disk; |
607c2a72 KZ |
1184 | |
1185 | if (force_all_parallel) | |
1186 | return 0; | |
1187 | ||
2b505124 | 1188 | if (instance_list && fs_is_stacked(instance_list->fs)) |
0c0f93fc | 1189 | /* any instance for a stacked device is already running */ |
607c2a72 | 1190 | return 1; |
607c2a72 | 1191 | |
2b505124 | 1192 | disk = fs_get_disk(fs, 1); |
0c0f93fc | 1193 | |
607c2a72 KZ |
1194 | /* |
1195 | * If we don't know the base device, assume that the device is | |
1196 | * already active if there are any fsck instances running. | |
0c0f93fc KZ |
1197 | * |
1198 | * Don't check a stacked device with any other disk too. | |
607c2a72 | 1199 | */ |
2b505124 | 1200 | if (!disk || fs_is_stacked(fs)) |
607c2a72 | 1201 | return (instance_list != 0); |
0c0f93fc | 1202 | |
607c2a72 | 1203 | for (inst = instance_list; inst; inst = inst->next) { |
2b505124 KZ |
1204 | dev_t idisk = fs_get_disk(inst->fs, 0); |
1205 | ||
1206 | if (!idisk || disk == idisk) | |
607c2a72 | 1207 | return 1; |
607c2a72 | 1208 | } |
ac8f2843 | 1209 | |
607c2a72 KZ |
1210 | return 0; |
1211 | } | |
1212 | ||
1213 | /* Check all file systems, using the /etc/fstab table. */ | |
2b505124 | 1214 | static int check_all(void) |
607c2a72 | 1215 | { |
607c2a72 KZ |
1216 | int not_done_yet = 1; |
1217 | int passno = 1; | |
1218 | int pass_done; | |
2b505124 | 1219 | int status = FSCK_EX_OK; |
607c2a72 | 1220 | |
2b505124 KZ |
1221 | struct libmnt_fs *fs; |
1222 | struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD); | |
1223 | ||
1224 | if (!itr) | |
1225 | err(FSCK_EX_ERROR, _("failed to allocate iterator")); | |
607c2a72 KZ |
1226 | |
1227 | /* | |
1228 | * Do an initial scan over the filesystem; mark filesystems | |
1229 | * which should be ignored as done, and resolve any "auto" | |
1230 | * filesystem types (done as a side-effect of calling ignore()). | |
1231 | */ | |
2b505124 KZ |
1232 | while (mnt_table_next_fs(fstab, itr, &fs) == 0) { |
1233 | if (ignore(fs)) { | |
1234 | fs_set_done(fs); | |
1235 | continue; | |
1236 | } | |
607c2a72 KZ |
1237 | } |
1238 | ||
2b505124 KZ |
1239 | if (verbose) |
1240 | fputs(_("Checking all file systems.\n"), stdout); | |
1241 | ||
607c2a72 KZ |
1242 | /* |
1243 | * Find and check the root filesystem. | |
1244 | */ | |
1245 | if (!parallel_root) { | |
2b505124 | 1246 | fs = mnt_table_find_target(fstab, "/", MNT_ITER_FORWARD); |
607c2a72 | 1247 | if (fs) { |
2b505124 KZ |
1248 | if (!skip_root && |
1249 | !fs_is_done(fs) && | |
67f09eae | 1250 | !(ignore_mounted && is_mounted(fs))) { |
6c6f2af9 | 1251 | status |= fsck_device(fs, 1); |
607c2a72 | 1252 | status |= wait_many(FLAG_WAIT_ALL); |
2b505124 KZ |
1253 | if (status > FSCK_EX_NONDESTRUCT) { |
1254 | mnt_free_iter(itr); | |
607c2a72 | 1255 | return status; |
2b505124 | 1256 | } |
607c2a72 | 1257 | } |
2b505124 | 1258 | fs_set_done(fs); |
607c2a72 KZ |
1259 | } |
1260 | } | |
2b505124 | 1261 | |
607c2a72 KZ |
1262 | /* |
1263 | * This is for the bone-headed user who enters the root | |
1264 | * filesystem twice. Skip root will skep all root entries. | |
1265 | */ | |
2b505124 KZ |
1266 | if (skip_root) { |
1267 | mnt_reset_iter(itr, MNT_ITER_FORWARD); | |
1268 | ||
1269 | while(mnt_table_next_fs(fstab, itr, &fs) == 0) { | |
1270 | const char *tgt = mnt_fs_get_target(fs); | |
1271 | ||
1272 | if (tgt && strcmp(tgt, "/") == 0) | |
1273 | fs_set_done(fs); | |
1274 | } | |
1275 | } | |
607c2a72 KZ |
1276 | |
1277 | while (not_done_yet) { | |
1278 | not_done_yet = 0; | |
1279 | pass_done = 1; | |
1280 | ||
2b505124 KZ |
1281 | mnt_reset_iter(itr, MNT_ITER_FORWARD); |
1282 | ||
1283 | while(mnt_table_next_fs(fstab, itr, &fs) == 0) { | |
1284 | ||
607c2a72 KZ |
1285 | if (cancel_requested) |
1286 | break; | |
2b505124 | 1287 | if (fs_is_done(fs)) |
607c2a72 KZ |
1288 | continue; |
1289 | /* | |
1290 | * If the filesystem's pass number is higher | |
1291 | * than the current pass number, then we don't | |
1292 | * do it yet. | |
1293 | */ | |
2b505124 | 1294 | if (mnt_fs_get_passno(fs) > passno) { |
607c2a72 KZ |
1295 | not_done_yet++; |
1296 | continue; | |
1297 | } | |
67f09eae | 1298 | if (ignore_mounted && is_mounted(fs)) { |
2b505124 | 1299 | fs_set_done(fs); |
607c2a72 KZ |
1300 | continue; |
1301 | } | |
1302 | /* | |
1303 | * If a filesystem on a particular device has | |
1304 | * already been spawned, then we need to defer | |
1305 | * this to another pass. | |
1306 | */ | |
0c0f93fc | 1307 | if (disk_already_active(fs)) { |
607c2a72 KZ |
1308 | pass_done = 0; |
1309 | continue; | |
1310 | } | |
1311 | /* | |
1312 | * Spawn off the fsck process | |
1313 | */ | |
6c6f2af9 | 1314 | status |= fsck_device(fs, serialize); |
2b505124 | 1315 | fs_set_done(fs); |
607c2a72 KZ |
1316 | |
1317 | /* | |
1318 | * Only do one filesystem at a time, or if we | |
1319 | * have a limit on the number of fsck's extant | |
1320 | * at one time, apply that limit. | |
1321 | */ | |
1322 | if (serialize || | |
1323 | (max_running && (num_running >= max_running))) { | |
1324 | pass_done = 0; | |
1325 | break; | |
1326 | } | |
1327 | } | |
1328 | if (cancel_requested) | |
1329 | break; | |
1330 | if (verbose > 1) | |
1331 | printf(_("--waiting-- (pass %d)\n"), passno); | |
2b505124 | 1332 | |
607c2a72 KZ |
1333 | status |= wait_many(pass_done ? FLAG_WAIT_ALL : |
1334 | FLAG_WAIT_ATLEAST_ONE); | |
1335 | if (pass_done) { | |
1336 | if (verbose > 1) | |
1337 | printf("----------------------------------\n"); | |
1338 | passno++; | |
1339 | } else | |
1340 | not_done_yet++; | |
1341 | } | |
2b505124 | 1342 | |
607c2a72 KZ |
1343 | if (cancel_requested && !kill_sent) { |
1344 | kill_all(SIGTERM); | |
1345 | kill_sent++; | |
1346 | } | |
2b505124 | 1347 | |
607c2a72 | 1348 | status |= wait_many(FLAG_WAIT_ATLEAST_ONE); |
2b505124 | 1349 | mnt_free_iter(itr); |
607c2a72 KZ |
1350 | return status; |
1351 | } | |
1352 | ||
1b8ce047 | 1353 | static void __attribute__((__noreturn__)) usage(FILE *out) |
607c2a72 | 1354 | { |
1b8ce047 KZ |
1355 | fputs(USAGE_HEADER, out); |
1356 | fprintf(out, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"), | |
1357 | program_invocation_short_name); | |
1358 | ||
1359 | fputs(USAGE_OPTIONS, out); | |
1360 | fputs(_(" -A check all filesystems\n"), out); | |
1361 | fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out); | |
1362 | fputs(_(" -l lock the device to guarantee exclusive access\n"), out); | |
1363 | fputs(_(" -M do not check mounted filesystems\n"), out); | |
1364 | fputs(_(" -N do not execute, just show what would be done\n"), out); | |
1365 | fputs(_(" -P check filesystems in parallel, including root\n"), out); | |
1366 | fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out); | |
1367 | fputs(_(" -r report statistics for each device checked\n"), out); | |
1368 | fputs(_(" -s serialize the checking operations\n"), out); | |
1369 | fputs(_(" -T do not show the title on startup\n"), out); | |
1370 | fputs(_(" -t <type> specify filesystem types to be checked;\n" | |
1371 | " <type> is allowed to be a comma-separated list\n"), out); | |
1372 | fputs(_(" -V explain what is being done\n"), out); | |
1373 | fputs(_(" -? display this help and exit\n"), out); | |
1374 | ||
1375 | fputs(USAGE_SEPARATOR, out); | |
1376 | fputs(_("See the specific fsck.* commands for available fs-options."), out); | |
1377 | fprintf(out, USAGE_MAN_TAIL("fsck(8)")); | |
1378 | ||
1379 | exit(out == stderr ? FSCK_EX_USAGE : FSCK_EX_OK); | |
607c2a72 KZ |
1380 | } |
1381 | ||
7ac166bf | 1382 | static void signal_cancel(int sig __attribute__((__unused__))) |
607c2a72 KZ |
1383 | { |
1384 | cancel_requested++; | |
1385 | } | |
1386 | ||
9895daca | 1387 | static void parse_argv(int argc, char *argv[]) |
607c2a72 KZ |
1388 | { |
1389 | int i, j; | |
1390 | char *arg, *dev, *tmp = 0; | |
1391 | char options[128]; | |
1392 | int opt = 0; | |
1393 | int opts_for_fsck = 0; | |
1394 | struct sigaction sa; | |
1395 | ||
1396 | /* | |
1397 | * Set up signal action | |
1398 | */ | |
1399 | memset(&sa, 0, sizeof(struct sigaction)); | |
1400 | sa.sa_handler = signal_cancel; | |
1401 | sigaction(SIGINT, &sa, 0); | |
1402 | sigaction(SIGTERM, &sa, 0); | |
1403 | ||
1404 | num_devices = 0; | |
1405 | num_args = 0; | |
1406 | instance_list = 0; | |
1407 | ||
607c2a72 KZ |
1408 | for (i=1; i < argc; i++) { |
1409 | arg = argv[i]; | |
1410 | if (!arg) | |
1411 | continue; | |
1412 | if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) { | |
0a09eb4e | 1413 | if (num_devices >= MAX_DEVICES) |
947558f5 | 1414 | errx(FSCK_EX_ERROR, _("too many devices")); |
e33b39a8 KZ |
1415 | |
1416 | dev = mnt_resolve_spec(arg, mntcache); | |
1417 | ||
607c2a72 KZ |
1418 | if (!dev && strchr(arg, '=')) { |
1419 | /* | |
1420 | * Check to see if we failed because | |
1421 | * /proc/partitions isn't found. | |
1422 | */ | |
fb429f22 | 1423 | if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) { |
289dcc90 | 1424 | warn(_("cannot open %s"), |
0a09eb4e | 1425 | _PATH_PROC_PARTITIONS); |
947558f5 | 1426 | errx(FSCK_EX_ERROR, _("Is /proc mounted?")); |
607c2a72 KZ |
1427 | } |
1428 | /* | |
1429 | * Check to see if this is because | |
1430 | * we're not running as root | |
1431 | */ | |
1432 | if (geteuid()) | |
947558f5 | 1433 | errx(FSCK_EX_ERROR, |
0a09eb4e SK |
1434 | _("must be root to scan for matching filesystems: %s"), |
1435 | arg); | |
607c2a72 | 1436 | else |
947558f5 | 1437 | errx(FSCK_EX_ERROR, |
0a09eb4e SK |
1438 | _("couldn't find matching filesystem: %s"), |
1439 | arg); | |
607c2a72 | 1440 | } |
2a24e16e | 1441 | devices[num_devices++] = dev ? dev : xstrdup(arg); |
607c2a72 KZ |
1442 | continue; |
1443 | } | |
1444 | if (arg[0] != '-' || opts_for_fsck) { | |
0a09eb4e | 1445 | if (num_args >= MAX_ARGS) |
947558f5 | 1446 | errx(FSCK_EX_ERROR, _("too many arguments")); |
2a24e16e | 1447 | args[num_args++] = xstrdup(arg); |
607c2a72 KZ |
1448 | continue; |
1449 | } | |
1450 | for (j=1; arg[j]; j++) { | |
1451 | if (opts_for_fsck) { | |
1452 | options[++opt] = arg[j]; | |
1453 | continue; | |
1454 | } | |
1455 | switch (arg[j]) { | |
1456 | case 'A': | |
0a09eb4e | 1457 | doall = 1; |
607c2a72 KZ |
1458 | break; |
1459 | case 'C': | |
0a09eb4e | 1460 | progress = 1; |
607c2a72 KZ |
1461 | if (arg[j+1]) { |
1462 | progress_fd = string_to_int(arg+j+1); | |
1463 | if (progress_fd < 0) | |
1464 | progress_fd = 0; | |
1465 | else | |
1466 | goto next_arg; | |
1467 | } else if ((i+1) < argc && | |
1468 | !strncmp(argv[i+1], "-", 1) == 0) { | |
1469 | progress_fd = string_to_int(argv[i]); | |
1470 | if (progress_fd < 0) | |
1471 | progress_fd = 0; | |
1472 | else { | |
f1c2eaac | 1473 | ++i; |
607c2a72 | 1474 | goto next_arg; |
607c2a72 KZ |
1475 | } |
1476 | } | |
1477 | break; | |
dd0bd943 | 1478 | case 'l': |
0a09eb4e | 1479 | lockdisk = 1; |
dd0bd943 | 1480 | break; |
607c2a72 KZ |
1481 | case 'V': |
1482 | verbose++; | |
1483 | break; | |
1484 | case 'N': | |
0a09eb4e | 1485 | noexecute = 1; |
607c2a72 KZ |
1486 | break; |
1487 | case 'R': | |
0a09eb4e | 1488 | skip_root = 1; |
607c2a72 KZ |
1489 | break; |
1490 | case 'T': | |
0a09eb4e | 1491 | notitle = 1; |
607c2a72 KZ |
1492 | break; |
1493 | case 'M': | |
0a09eb4e | 1494 | ignore_mounted = 1; |
607c2a72 KZ |
1495 | break; |
1496 | case 'P': | |
0a09eb4e | 1497 | parallel_root = 1; |
607c2a72 | 1498 | break; |
5a0da00a FM |
1499 | case 'r': |
1500 | report_stats = 1; | |
1501 | break; | |
607c2a72 | 1502 | case 's': |
0a09eb4e | 1503 | serialize = 1; |
607c2a72 KZ |
1504 | break; |
1505 | case 't': | |
1506 | tmp = 0; | |
1507 | if (fstype) | |
1b8ce047 | 1508 | usage(stderr); |
607c2a72 KZ |
1509 | if (arg[j+1]) |
1510 | tmp = arg+j+1; | |
1511 | else if ((i+1) < argc) | |
1512 | tmp = argv[++i]; | |
1513 | else | |
1b8ce047 | 1514 | usage(stderr); |
2a24e16e | 1515 | fstype = xstrdup(tmp); |
607c2a72 KZ |
1516 | compile_fs_type(fstype, &fs_type_compiled); |
1517 | goto next_arg; | |
1518 | case '-': | |
1519 | opts_for_fsck++; | |
1520 | break; | |
1521 | case '?': | |
1b8ce047 | 1522 | usage(stdout); |
607c2a72 KZ |
1523 | break; |
1524 | default: | |
1525 | options[++opt] = arg[j]; | |
1526 | break; | |
1527 | } | |
1528 | } | |
1529 | next_arg: | |
1530 | if (opt) { | |
1531 | options[0] = '-'; | |
1532 | options[++opt] = '\0'; | |
0a09eb4e | 1533 | if (num_args >= MAX_ARGS) |
947558f5 | 1534 | errx(FSCK_EX_ERROR, _("too many arguments")); |
2a24e16e | 1535 | args[num_args++] = xstrdup(options); |
607c2a72 KZ |
1536 | opt = 0; |
1537 | } | |
1538 | } | |
1539 | if (getenv("FSCK_FORCE_ALL_PARALLEL")) | |
1540 | force_all_parallel++; | |
1541 | if ((tmp = getenv("FSCK_MAX_INST"))) | |
1542 | max_running = atoi(tmp); | |
1543 | } | |
1544 | ||
1545 | int main(int argc, char *argv[]) | |
1546 | { | |
1547 | int i, status = 0; | |
1548 | int interactive = 0; | |
1549 | char *oldpath = getenv("PATH"); | |
2b505124 | 1550 | struct libmnt_fs *fs; |
607c2a72 KZ |
1551 | |
1552 | setvbuf(stdout, NULL, _IONBF, BUFSIZ); | |
1553 | setvbuf(stderr, NULL, _IONBF, BUFSIZ); | |
1554 | ||
1555 | setlocale(LC_MESSAGES, ""); | |
1556 | setlocale(LC_CTYPE, ""); | |
1557 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1558 | textdomain(PACKAGE); | |
45ca68ec | 1559 | atexit(close_stdout); |
607c2a72 | 1560 | |
e33b39a8 KZ |
1561 | mnt_init_debug(0); /* init libmount debug mask */ |
1562 | mntcache = mnt_new_cache(); /* no fatal error if failed */ | |
1563 | ||
9895daca | 1564 | parse_argv(argc, argv); |
607c2a72 KZ |
1565 | |
1566 | if (!notitle) | |
e421313d | 1567 | printf(UTIL_LINUX_VERSION); |
607c2a72 | 1568 | |
2b505124 | 1569 | load_fs_info(); |
607c2a72 KZ |
1570 | |
1571 | /* Update our search path to include uncommon directories. */ | |
1572 | if (oldpath) { | |
0a09eb4e | 1573 | fsck_path = xmalloc (strlen (fsck_prefix_path) + 1 + |
607c2a72 | 1574 | strlen (oldpath) + 1); |
607c2a72 KZ |
1575 | strcpy (fsck_path, fsck_prefix_path); |
1576 | strcat (fsck_path, ":"); | |
1577 | strcat (fsck_path, oldpath); | |
1578 | } else { | |
2a24e16e | 1579 | fsck_path = xstrdup(fsck_prefix_path); |
607c2a72 KZ |
1580 | } |
1581 | ||
1582 | if ((num_devices == 1) || (serialize)) | |
1583 | interactive = 1; | |
1584 | ||
dd0bd943 | 1585 | if (lockdisk && (doall || num_devices > 1)) { |
0a09eb4e SK |
1586 | warnx(_("the -l option can be used with one " |
1587 | "device only -- ignore")); | |
dd0bd943 KZ |
1588 | lockdisk = 0; |
1589 | } | |
1590 | ||
607c2a72 KZ |
1591 | /* If -A was specified ("check all"), do that! */ |
1592 | if (doall) | |
1593 | return check_all(); | |
1594 | ||
1595 | if (num_devices == 0) { | |
1596 | serialize++; | |
1597 | interactive++; | |
1598 | return check_all(); | |
1599 | } | |
1600 | for (i = 0 ; i < num_devices; i++) { | |
1601 | if (cancel_requested) { | |
1602 | if (!kill_sent) { | |
1603 | kill_all(SIGTERM); | |
1604 | kill_sent++; | |
1605 | } | |
1606 | break; | |
1607 | } | |
1608 | fs = lookup(devices[i]); | |
2b505124 KZ |
1609 | if (!fs) |
1610 | fs = add_dummy_fs(devices[i]); | |
1611 | else if (fs_ignored_type(fs)) | |
e3174198 | 1612 | continue; |
67f09eae | 1613 | if (ignore_mounted && is_mounted(fs)) |
607c2a72 | 1614 | continue; |
6c6f2af9 | 1615 | status |= fsck_device(fs, interactive); |
607c2a72 KZ |
1616 | if (serialize || |
1617 | (max_running && (num_running >= max_running))) { | |
1618 | struct fsck_instance *inst; | |
1619 | ||
1620 | inst = wait_one(0); | |
1621 | if (inst) { | |
1622 | status |= inst->exit_status; | |
1623 | free_instance(inst); | |
1624 | } | |
1625 | if (verbose > 1) | |
1626 | printf("----------------------------------\n"); | |
1627 | } | |
1628 | } | |
1629 | status |= wait_many(FLAG_WAIT_ALL); | |
1630 | free(fsck_path); | |
6195f9e6 | 1631 | mnt_unref_cache(mntcache); |
50fccba1 KZ |
1632 | mnt_unref_table(fstab); |
1633 | mnt_unref_table(mtab); | |
607c2a72 KZ |
1634 | return status; |
1635 | } |