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