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