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