PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/external/e2fsprogs/misc/fsck.c

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