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