3
0

fsck.c 28 KB

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