fsck.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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 source tree.
  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. strcpy_and_process_escape_sequences(word, word);
  309. }
  310. static void free_instance(struct fsck_instance *i)
  311. {
  312. if (i->prog)
  313. free(i->prog);
  314. if (i->device)
  315. free(i->device);
  316. if (i->base_device)
  317. free(i->base_device);
  318. free(i);
  319. }
  320. static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
  321. const char *type, const char *opts,
  322. int freq, int passno)
  323. {
  324. struct fs_info *fs;
  325. fs = xmalloc(sizeof(struct fs_info));
  326. fs->device = string_copy(device);
  327. fs->mountpt = string_copy(mntpnt);
  328. fs->type = string_copy(type);
  329. fs->opts = string_copy(opts ? opts : "");
  330. fs->freq = freq;
  331. fs->passno = passno;
  332. fs->flags = 0;
  333. fs->next = NULL;
  334. if (!filesys_info)
  335. filesys_info = fs;
  336. else
  337. filesys_last->next = fs;
  338. filesys_last = fs;
  339. return fs;
  340. }
  341. static int parse_fstab_line(char *line, struct fs_info **ret_fs)
  342. {
  343. char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
  344. struct fs_info *fs;
  345. *ret_fs = 0;
  346. strip_line(line);
  347. if ((cp = strchr(line, '#')))
  348. *cp = 0; /* Ignore everything after the comment char */
  349. cp = line;
  350. device = parse_word(&cp);
  351. mntpnt = parse_word(&cp);
  352. type = parse_word(&cp);
  353. opts = parse_word(&cp);
  354. freq = parse_word(&cp);
  355. passno = parse_word(&cp);
  356. if (!device)
  357. return 0; /* Allow blank lines */
  358. if (!mntpnt || !type)
  359. return -1;
  360. parse_escape(device);
  361. parse_escape(mntpnt);
  362. parse_escape(type);
  363. parse_escape(opts);
  364. parse_escape(freq);
  365. parse_escape(passno);
  366. dev = blkid_get_devname(cache, device, NULL);
  367. if (dev)
  368. device = dev;
  369. if (strchr(type, ','))
  370. type = 0;
  371. fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
  372. freq ? atoi(freq) : -1,
  373. passno ? atoi(passno) : -1);
  374. if (dev)
  375. free(dev);
  376. if (!fs)
  377. return -1;
  378. *ret_fs = fs;
  379. return 0;
  380. }
  381. static void interpret_type(struct fs_info *fs)
  382. {
  383. char *t;
  384. if (strcmp(fs->type, "auto") != 0)
  385. return;
  386. t = blkid_get_tag_value(cache, "TYPE", fs->device);
  387. if (t) {
  388. free(fs->type);
  389. fs->type = t;
  390. }
  391. }
  392. /*
  393. * Load the filesystem database from /etc/fstab
  394. */
  395. static void load_fs_info(const char *filename)
  396. {
  397. FILE *f;
  398. char buf[1024];
  399. int lineno = 0;
  400. int old_fstab = 1;
  401. struct fs_info *fs;
  402. if ((f = fopen_or_warn(filename, "r")) == NULL) {
  403. return;
  404. }
  405. while (!feof(f)) {
  406. lineno++;
  407. if (!fgets(buf, sizeof(buf), f))
  408. break;
  409. buf[sizeof(buf)-1] = 0;
  410. if (parse_fstab_line(buf, &fs) < 0) {
  411. bb_error_msg("WARNING: bad format "
  412. "on line %d of %s\n", lineno, filename);
  413. continue;
  414. }
  415. if (!fs)
  416. continue;
  417. if (fs->passno < 0)
  418. fs->passno = 0;
  419. else
  420. old_fstab = 0;
  421. }
  422. fclose(f);
  423. if (old_fstab) {
  424. fputs("\007\007\007"
  425. "WARNING: Your /etc/fstab does not contain the fsck passno\n"
  426. " field. I will kludge around things for you, but you\n"
  427. " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
  428. for (fs = filesys_info; fs; fs = fs->next) {
  429. fs->passno = 1;
  430. }
  431. }
  432. }
  433. /* Lookup filesys in /etc/fstab and return the corresponding entry. */
  434. static struct fs_info *lookup(char *filesys)
  435. {
  436. struct fs_info *fs;
  437. /* No filesys name given. */
  438. if (filesys == NULL)
  439. return NULL;
  440. for (fs = filesys_info; fs; fs = fs->next) {
  441. if (!strcmp(filesys, fs->device) ||
  442. (fs->mountpt && !strcmp(filesys, fs->mountpt)))
  443. break;
  444. }
  445. return fs;
  446. }
  447. /* Find fsck program for a given fs type. */
  448. static char *find_fsck(char *type)
  449. {
  450. char *s;
  451. const char *tpl;
  452. char *p = string_copy(fsck_path);
  453. struct stat st;
  454. /* Are we looking for a program or just a type? */
  455. tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
  456. for (s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
  457. s = xasprintf(tpl, s, type);
  458. if (stat(s, &st) == 0) break;
  459. free(s);
  460. }
  461. free(p);
  462. return s;
  463. }
  464. static int progress_active(void)
  465. {
  466. struct fsck_instance *inst;
  467. for (inst = instance_list; inst; inst = inst->next) {
  468. if (inst->flags & FLAG_DONE)
  469. continue;
  470. if (inst->flags & FLAG_PROGRESS)
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. /*
  476. * Execute a particular fsck program, and link it into the list of
  477. * child processes we are waiting for.
  478. */
  479. static int execute(const char *type, const char *device, const char *mntpt,
  480. int interactive)
  481. {
  482. char *s, *argv[80];
  483. char *prog;
  484. int argc, i;
  485. struct fsck_instance *inst, *p;
  486. pid_t pid;
  487. inst = xzalloc(sizeof(struct fsck_instance));
  488. prog = xasprintf("fsck.%s", type);
  489. argv[0] = prog;
  490. argc = 1;
  491. for (i=0; i <num_args; i++)
  492. argv[argc++] = string_copy(args[i]);
  493. if (progress && !progress_active()) {
  494. if ((strcmp(type, "ext2") == 0) ||
  495. (strcmp(type, "ext3") == 0)) {
  496. char tmp[80];
  497. snprintf(tmp, 80, "-C%d", progress_fd);
  498. argv[argc++] = string_copy(tmp);
  499. inst->flags |= FLAG_PROGRESS;
  500. }
  501. }
  502. argv[argc++] = string_copy(device);
  503. argv[argc] = 0;
  504. s = find_fsck(prog);
  505. if (s == NULL) {
  506. bb_error_msg("%s: not found", prog);
  507. return ENOENT;
  508. }
  509. if (verbose || noexecute) {
  510. printf("[%s (%d) -- %s] ", s, num_running,
  511. mntpt ? mntpt : device);
  512. for (i=0; i < argc; i++)
  513. printf("%s ", argv[i]);
  514. bb_putchar('\n');
  515. }
  516. /* Fork and execute the correct program. */
  517. if (noexecute)
  518. pid = -1;
  519. else if ((pid = fork()) < 0) {
  520. perror("vfork"+1);
  521. return errno;
  522. } else if (pid == 0) {
  523. if (!interactive)
  524. close(0);
  525. (void) execv(s, argv);
  526. bb_simple_perror_msg_and_die(argv[0]);
  527. }
  528. for (i = 1; i < argc; i++)
  529. free(argv[i]);
  530. free(s);
  531. inst->pid = pid;
  532. inst->prog = prog;
  533. inst->type = string_copy(type);
  534. inst->device = string_copy(device);
  535. inst->base_device = base_device(device);
  536. inst->start_time = time(0);
  537. inst->next = NULL;
  538. /*
  539. * Find the end of the list, so we add the instance on at the end.
  540. */
  541. for (p = instance_list; p && p->next; p = p->next);
  542. if (p)
  543. p->next = inst;
  544. else
  545. instance_list = inst;
  546. return 0;
  547. }
  548. /*
  549. * Send a signal to all outstanding fsck child processes
  550. */
  551. static int kill_all(int signum)
  552. {
  553. struct fsck_instance *inst;
  554. int n = 0;
  555. for (inst = instance_list; inst; inst = inst->next) {
  556. if (inst->flags & FLAG_DONE)
  557. continue;
  558. kill(inst->pid, signum);
  559. n++;
  560. }
  561. return n;
  562. }
  563. /*
  564. * Wait for one child process to exit; when it does, unlink it from
  565. * the list of executing child processes, and return it.
  566. */
  567. static struct fsck_instance *wait_one(int flags)
  568. {
  569. int status;
  570. int sig;
  571. struct fsck_instance *inst, *inst2, *prev;
  572. pid_t pid;
  573. if (!instance_list)
  574. return NULL;
  575. if (noexecute) {
  576. inst = instance_list;
  577. prev = 0;
  578. #ifdef RANDOM_DEBUG
  579. while (inst->next && (random() & 1)) {
  580. prev = inst;
  581. inst = inst->next;
  582. }
  583. #endif
  584. inst->exit_status = 0;
  585. goto ret_inst;
  586. }
  587. /*
  588. * gcc -Wall fails saving throw against stupidity
  589. * (inst and prev are thought to be uninitialized variables)
  590. */
  591. inst = prev = NULL;
  592. do {
  593. pid = waitpid(-1, &status, flags);
  594. if (cancel_requested && !kill_sent) {
  595. kill_all(SIGTERM);
  596. kill_sent++;
  597. }
  598. if ((pid == 0) && (flags & WNOHANG))
  599. return NULL;
  600. if (pid < 0) {
  601. if ((errno == EINTR) || (errno == EAGAIN))
  602. continue;
  603. if (errno == ECHILD) {
  604. bb_error_msg("wait: no more child process?!?");
  605. return NULL;
  606. }
  607. perror("wait");
  608. continue;
  609. }
  610. for (prev = 0, inst = instance_list;
  611. inst;
  612. prev = inst, inst = inst->next) {
  613. if (inst->pid == pid)
  614. break;
  615. }
  616. } while (!inst);
  617. if (WIFEXITED(status))
  618. status = WEXITSTATUS(status);
  619. else if (WIFSIGNALED(status)) {
  620. sig = WTERMSIG(status);
  621. if (sig == SIGINT) {
  622. status = EXIT_UNCORRECTED;
  623. } else {
  624. printf("Warning... %s for device %s exited "
  625. "with signal %d.\n",
  626. inst->prog, inst->device, sig);
  627. status = EXIT_ERROR;
  628. }
  629. } else {
  630. printf("%s %s: status is %x, should never happen.\n",
  631. inst->prog, inst->device, status);
  632. status = EXIT_ERROR;
  633. }
  634. inst->exit_status = status;
  635. if (progress && (inst->flags & FLAG_PROGRESS) &&
  636. !progress_active()) {
  637. for (inst2 = instance_list; inst2; inst2 = inst2->next) {
  638. if (inst2->flags & FLAG_DONE)
  639. continue;
  640. if (strcmp(inst2->type, "ext2") &&
  641. strcmp(inst2->type, "ext3"))
  642. continue;
  643. /*
  644. * If we've just started the fsck, wait a tiny
  645. * bit before sending the kill, to give it
  646. * time to set up the signal handler
  647. */
  648. if (inst2->start_time < time(0)+2) {
  649. if (fork() == 0) {
  650. sleep(1);
  651. kill(inst2->pid, SIGUSR1);
  652. exit(0);
  653. }
  654. } else
  655. kill(inst2->pid, SIGUSR1);
  656. inst2->flags |= FLAG_PROGRESS;
  657. break;
  658. }
  659. }
  660. ret_inst:
  661. if (prev)
  662. prev->next = inst->next;
  663. else
  664. instance_list = inst->next;
  665. if (verbose > 1)
  666. printf("Finished with %s (exit status %d)\n",
  667. inst->device, inst->exit_status);
  668. num_running--;
  669. return inst;
  670. }
  671. #define FLAG_WAIT_ALL 0
  672. #define FLAG_WAIT_ATLEAST_ONE 1
  673. /*
  674. * Wait until all executing child processes have exited; return the
  675. * logical OR of all of their exit code values.
  676. */
  677. static int wait_many(int flags)
  678. {
  679. struct fsck_instance *inst;
  680. int global_status = 0;
  681. int wait_flags = 0;
  682. while ((inst = wait_one(wait_flags))) {
  683. global_status |= inst->exit_status;
  684. free_instance(inst);
  685. #ifdef RANDOM_DEBUG
  686. if (noexecute && (flags & WNOHANG) && !(random() % 3))
  687. break;
  688. #endif
  689. if (flags & FLAG_WAIT_ATLEAST_ONE)
  690. wait_flags = WNOHANG;
  691. }
  692. return global_status;
  693. }
  694. /*
  695. * Run the fsck program on a particular device
  696. *
  697. * If the type is specified using -t, and it isn't prefixed with "no"
  698. * (as in "noext2") and only one filesystem type is specified, then
  699. * use that type regardless of what is specified in /etc/fstab.
  700. *
  701. * If the type isn't specified by the user, then use either the type
  702. * specified in /etc/fstab, or DEFAULT_FSTYPE.
  703. */
  704. static void fsck_device(struct fs_info *fs, int interactive)
  705. {
  706. const char *type;
  707. int retval;
  708. interpret_type(fs);
  709. if (strcmp(fs->type, "auto") != 0)
  710. type = fs->type;
  711. else if (fstype && strncmp(fstype, "no", 2) &&
  712. strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
  713. !strchr(fstype, ','))
  714. type = fstype;
  715. else
  716. type = DEFAULT_FSTYPE;
  717. num_running++;
  718. retval = execute(type, fs->device, fs->mountpt, interactive);
  719. if (retval) {
  720. bb_error_msg("error %d while executing fsck.%s for %s",
  721. retval, type, fs->device);
  722. num_running--;
  723. }
  724. }
  725. /*
  726. * Deal with the fsck -t argument.
  727. */
  728. struct fs_type_compile {
  729. char **list;
  730. int *type;
  731. int negate;
  732. } fs_type_compiled;
  733. #define FS_TYPE_NORMAL 0
  734. #define FS_TYPE_OPT 1
  735. #define FS_TYPE_NEGOPT 2
  736. static const char fs_type_syntax_error[] =
  737. "Either all or none of the filesystem types passed to -t must be prefixed\n"
  738. "with 'no' or '!'.";
  739. static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
  740. {
  741. char *cp, *list, *s;
  742. int num = 2;
  743. int negate, first_negate = 1;
  744. if (fs_type) {
  745. for (cp=fs_type; *cp; cp++) {
  746. if (*cp == ',')
  747. num++;
  748. }
  749. }
  750. cmp->list = xzalloc(num * sizeof(char *));
  751. cmp->type = xzalloc(num * sizeof(int));
  752. cmp->negate = 0;
  753. if (!fs_type)
  754. return;
  755. list = string_copy(fs_type);
  756. num = 0;
  757. s = strtok(list, ",");
  758. while (s) {
  759. negate = 0;
  760. if (strncmp(s, "no", 2) == 0) {
  761. s += 2;
  762. negate = 1;
  763. } else if (*s == '!') {
  764. s++;
  765. negate = 1;
  766. }
  767. if (strcmp(s, "loop") == 0)
  768. /* loop is really short-hand for opts=loop */
  769. goto loop_special_case;
  770. else if (strncmp(s, "opts=", 5) == 0) {
  771. s += 5;
  772. loop_special_case:
  773. cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
  774. } else {
  775. if (first_negate) {
  776. cmp->negate = negate;
  777. first_negate = 0;
  778. }
  779. if ((negate && !cmp->negate) ||
  780. (!negate && cmp->negate)) {
  781. bb_error_msg_and_die("%s", fs_type_syntax_error);
  782. }
  783. }
  784. cmp->list[num++] = string_copy(s);
  785. s = strtok(NULL, ",");
  786. }
  787. free(list);
  788. }
  789. /*
  790. * This function returns true if a particular option appears in a
  791. * comma-delimited options list
  792. */
  793. static int opt_in_list(char *opt, char *optlist)
  794. {
  795. char *list, *s;
  796. if (!optlist)
  797. return 0;
  798. list = string_copy(optlist);
  799. s = strtok(list, ",");
  800. while (s) {
  801. if (strcmp(s, opt) == 0) {
  802. free(list);
  803. return 1;
  804. }
  805. s = strtok(NULL, ",");
  806. }
  807. free(list);
  808. return 0;
  809. }
  810. /* See if the filesystem matches the criteria given by the -t option */
  811. static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
  812. {
  813. int n, ret = 0, checked_type = 0;
  814. char *cp;
  815. if (cmp->list == 0 || cmp->list[0] == 0)
  816. return 1;
  817. for (n=0; (cp = cmp->list[n]); n++) {
  818. switch (cmp->type[n]) {
  819. case FS_TYPE_NORMAL:
  820. checked_type++;
  821. if (strcmp(cp, fs->type) == 0) {
  822. ret = 1;
  823. }
  824. break;
  825. case FS_TYPE_NEGOPT:
  826. if (opt_in_list(cp, fs->opts))
  827. return 0;
  828. break;
  829. case FS_TYPE_OPT:
  830. if (!opt_in_list(cp, fs->opts))
  831. return 0;
  832. break;
  833. }
  834. }
  835. if (checked_type == 0)
  836. return 1;
  837. return (cmp->negate ? !ret : ret);
  838. }
  839. /* Check if we should ignore this filesystem. */
  840. static int ignore(struct fs_info *fs)
  841. {
  842. int wanted;
  843. char *s;
  844. /*
  845. * If the pass number is 0, ignore it.
  846. */
  847. if (fs->passno == 0)
  848. return 1;
  849. interpret_type(fs);
  850. /*
  851. * If a specific fstype is specified, and it doesn't match,
  852. * ignore it.
  853. */
  854. if (!fs_match(fs, &fs_type_compiled)) return 1;
  855. /* Are we ignoring this type? */
  856. if (index_in_str_array(ignored_types, fs->type) >= 0)
  857. return 1;
  858. /* Do we really really want to check this fs? */
  859. wanted = index_in_str_array(really_wanted, fs->type) >= 0;
  860. /* See if the <fsck.fs> program is available. */
  861. s = find_fsck(fs->type);
  862. if (s == NULL) {
  863. if (wanted)
  864. bb_error_msg("can't check %s: fsck.%s not found",
  865. fs->device, fs->type);
  866. return 1;
  867. }
  868. free(s);
  869. /* We can and want to check this file system type. */
  870. return 0;
  871. }
  872. /*
  873. * Returns TRUE if a partition on the same disk is already being
  874. * checked.
  875. */
  876. static int device_already_active(char *device)
  877. {
  878. struct fsck_instance *inst;
  879. char *base;
  880. if (force_all_parallel)
  881. return 0;
  882. #ifdef BASE_MD
  883. /* Don't check a soft raid disk with any other disk */
  884. if (instance_list &&
  885. (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
  886. !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
  887. return 1;
  888. #endif
  889. base = base_device(device);
  890. /*
  891. * If we don't know the base device, assume that the device is
  892. * already active if there are any fsck instances running.
  893. */
  894. if (!base)
  895. return (instance_list != 0);
  896. for (inst = instance_list; inst; inst = inst->next) {
  897. if (!inst->base_device || !strcmp(base, inst->base_device)) {
  898. free(base);
  899. return 1;
  900. }
  901. }
  902. free(base);
  903. return 0;
  904. }
  905. /* Check all file systems, using the /etc/fstab table. */
  906. static int check_all(void)
  907. {
  908. struct fs_info *fs = NULL;
  909. int status = EXIT_OK;
  910. int not_done_yet = 1;
  911. int passno = 1;
  912. int pass_done;
  913. if (verbose)
  914. fputs("Checking all file systems.\n", stdout);
  915. /*
  916. * Do an initial scan over the filesystem; mark filesystems
  917. * which should be ignored as done, and resolve any "auto"
  918. * filesystem types (done as a side-effect of calling ignore()).
  919. */
  920. for (fs = filesys_info; fs; fs = fs->next) {
  921. if (ignore(fs))
  922. fs->flags |= FLAG_DONE;
  923. }
  924. /*
  925. * Find and check the root filesystem.
  926. */
  927. if (!parallel_root) {
  928. for (fs = filesys_info; fs; fs = fs->next) {
  929. if (LONE_CHAR(fs->mountpt, '/'))
  930. break;
  931. }
  932. if (fs) {
  933. if (!skip_root && !ignore(fs)) {
  934. fsck_device(fs, 1);
  935. status |= wait_many(FLAG_WAIT_ALL);
  936. if (status > EXIT_NONDESTRUCT)
  937. return status;
  938. }
  939. fs->flags |= FLAG_DONE;
  940. }
  941. }
  942. /*
  943. * This is for the bone-headed user who enters the root
  944. * filesystem twice. Skip root will skep all root entries.
  945. */
  946. if (skip_root)
  947. for (fs = filesys_info; fs; fs = fs->next)
  948. if (LONE_CHAR(fs->mountpt, '/'))
  949. fs->flags |= FLAG_DONE;
  950. while (not_done_yet) {
  951. not_done_yet = 0;
  952. pass_done = 1;
  953. for (fs = filesys_info; fs; fs = fs->next) {
  954. if (cancel_requested)
  955. break;
  956. if (fs->flags & FLAG_DONE)
  957. continue;
  958. /*
  959. * If the filesystem's pass number is higher
  960. * than the current pass number, then we don't
  961. * do it yet.
  962. */
  963. if (fs->passno > passno) {
  964. not_done_yet++;
  965. continue;
  966. }
  967. /*
  968. * If a filesystem on a particular device has
  969. * already been spawned, then we need to defer
  970. * this to another pass.
  971. */
  972. if (device_already_active(fs->device)) {
  973. pass_done = 0;
  974. continue;
  975. }
  976. /*
  977. * Spawn off the fsck process
  978. */
  979. fsck_device(fs, serialize);
  980. fs->flags |= FLAG_DONE;
  981. /*
  982. * Only do one filesystem at a time, or if we
  983. * have a limit on the number of fsck's extant
  984. * at one time, apply that limit.
  985. */
  986. if (serialize ||
  987. (max_running && (num_running >= max_running))) {
  988. pass_done = 0;
  989. break;
  990. }
  991. }
  992. if (cancel_requested)
  993. break;
  994. if (verbose > 1)
  995. printf("--waiting-- (pass %d)\n", passno);
  996. status |= wait_many(pass_done ? FLAG_WAIT_ALL :
  997. FLAG_WAIT_ATLEAST_ONE);
  998. if (pass_done) {
  999. if (verbose > 1)
  1000. printf("----------------------------------\n");
  1001. passno++;
  1002. } else
  1003. not_done_yet++;
  1004. }
  1005. if (cancel_requested && !kill_sent) {
  1006. kill_all(SIGTERM);
  1007. kill_sent++;
  1008. }
  1009. status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
  1010. return status;
  1011. }
  1012. static void signal_cancel(int sig FSCK_ATTR((unused)))
  1013. {
  1014. cancel_requested++;
  1015. }
  1016. static void PRS(int argc, char **argv)
  1017. {
  1018. int i, j;
  1019. char *arg, *dev, *tmp = NULL;
  1020. char options[128];
  1021. int opt = 0;
  1022. int opts_for_fsck = 0;
  1023. struct sigaction sa;
  1024. /*
  1025. * Set up signal action
  1026. */
  1027. memset(&sa, 0, sizeof(struct sigaction));
  1028. sa.sa_handler = signal_cancel;
  1029. sigaction(SIGINT, &sa, 0);
  1030. sigaction(SIGTERM, &sa, 0);
  1031. num_devices = 0;
  1032. num_args = 0;
  1033. instance_list = 0;
  1034. for (i=1; i < argc; i++) {
  1035. arg = argv[i];
  1036. if (!arg)
  1037. continue;
  1038. if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
  1039. if (num_devices >= MAX_DEVICES) {
  1040. bb_error_msg_and_die("too many devices");
  1041. }
  1042. dev = blkid_get_devname(cache, arg, NULL);
  1043. if (!dev && strchr(arg, '=')) {
  1044. /*
  1045. * Check to see if we failed because
  1046. * /proc/partitions isn't found.
  1047. */
  1048. if (access("/proc/partitions", R_OK) < 0) {
  1049. bb_perror_msg_and_die("can't open /proc/partitions "
  1050. "(is /proc mounted?)");
  1051. }
  1052. /*
  1053. * Check to see if this is because
  1054. * we're not running as root
  1055. */
  1056. if (geteuid())
  1057. bb_error_msg_and_die(
  1058. "must be root to scan for matching filesystems: %s\n", arg);
  1059. else
  1060. bb_error_msg_and_die(
  1061. "can't find matching filesystem: %s", arg);
  1062. }
  1063. devices[num_devices++] = dev ? dev : string_copy(arg);
  1064. continue;
  1065. }
  1066. if (arg[0] != '-' || opts_for_fsck) {
  1067. if (num_args >= MAX_ARGS) {
  1068. bb_error_msg_and_die("too many arguments");
  1069. }
  1070. args[num_args++] = string_copy(arg);
  1071. continue;
  1072. }
  1073. for (j=1; arg[j]; j++) {
  1074. if (opts_for_fsck) {
  1075. options[++opt] = arg[j];
  1076. continue;
  1077. }
  1078. switch (arg[j]) {
  1079. case 'A':
  1080. doall++;
  1081. break;
  1082. case 'C':
  1083. progress++;
  1084. if (arg[j+1]) {
  1085. progress_fd = string_to_int(arg+j+1);
  1086. if (progress_fd < 0)
  1087. progress_fd = 0;
  1088. else
  1089. goto next_arg;
  1090. } else if ((i+1) < argc
  1091. && argv[i+1][0] != '-') {
  1092. progress_fd = string_to_int(argv[i]);
  1093. if (progress_fd < 0)
  1094. progress_fd = 0;
  1095. else {
  1096. goto next_arg;
  1097. i++;
  1098. }
  1099. }
  1100. break;
  1101. case 'V':
  1102. verbose++;
  1103. break;
  1104. case 'N':
  1105. noexecute++;
  1106. break;
  1107. case 'R':
  1108. skip_root++;
  1109. break;
  1110. case 'T':
  1111. notitle++;
  1112. break;
  1113. case 'M':
  1114. like_mount++;
  1115. break;
  1116. case 'P':
  1117. parallel_root++;
  1118. break;
  1119. case 's':
  1120. serialize++;
  1121. break;
  1122. case 't':
  1123. tmp = 0;
  1124. if (fstype)
  1125. bb_show_usage();
  1126. if (arg[j+1])
  1127. tmp = arg+j+1;
  1128. else if ((i+1) < argc)
  1129. tmp = argv[++i];
  1130. else
  1131. bb_show_usage();
  1132. fstype = string_copy(tmp);
  1133. compile_fs_type(fstype, &fs_type_compiled);
  1134. goto next_arg;
  1135. case '-':
  1136. opts_for_fsck++;
  1137. break;
  1138. case '?':
  1139. bb_show_usage();
  1140. break;
  1141. default:
  1142. options[++opt] = arg[j];
  1143. break;
  1144. }
  1145. }
  1146. next_arg:
  1147. if (opt) {
  1148. options[0] = '-';
  1149. options[++opt] = '\0';
  1150. if (num_args >= MAX_ARGS) {
  1151. bb_error_msg("too many arguments");
  1152. }
  1153. args[num_args++] = string_copy(options);
  1154. opt = 0;
  1155. }
  1156. }
  1157. if (getenv("FSCK_FORCE_ALL_PARALLEL"))
  1158. force_all_parallel++;
  1159. if ((tmp = getenv("FSCK_MAX_INST")))
  1160. max_running = atoi(tmp);
  1161. }
  1162. int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1163. int fsck_main(int argc, char **argv)
  1164. {
  1165. int i, status = 0;
  1166. int interactive = 0;
  1167. const char *fstab;
  1168. struct fs_info *fs;
  1169. setvbuf(stdout, NULL, _IONBF, BUFSIZ);
  1170. setvbuf(stderr, NULL, _IONBF, BUFSIZ);
  1171. blkid_get_cache(&cache, NULL);
  1172. PRS(argc, argv);
  1173. if (!notitle)
  1174. printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
  1175. fstab = getenv("FSTAB_FILE");
  1176. if (!fstab)
  1177. fstab = _PATH_MNTTAB;
  1178. load_fs_info(fstab);
  1179. fsck_path = e2fs_set_sbin_path();
  1180. if ((num_devices == 1) || (serialize))
  1181. interactive = 1;
  1182. /* If -A was specified ("check all"), do that! */
  1183. if (doall)
  1184. return check_all();
  1185. if (num_devices == 0) {
  1186. serialize++;
  1187. interactive++;
  1188. return check_all();
  1189. }
  1190. for (i = 0; i < num_devices; i++) {
  1191. if (cancel_requested) {
  1192. if (!kill_sent) {
  1193. kill_all(SIGTERM);
  1194. kill_sent++;
  1195. }
  1196. break;
  1197. }
  1198. fs = lookup(devices[i]);
  1199. if (!fs) {
  1200. fs = create_fs_device(devices[i], 0, "auto",
  1201. 0, -1, -1);
  1202. if (!fs)
  1203. continue;
  1204. }
  1205. fsck_device(fs, interactive);
  1206. if (serialize ||
  1207. (max_running && (num_running >= max_running))) {
  1208. struct fsck_instance *inst;
  1209. inst = wait_one(0);
  1210. if (inst) {
  1211. status |= inst->exit_status;
  1212. free_instance(inst);
  1213. }
  1214. if (verbose > 1)
  1215. printf("----------------------------------\n");
  1216. }
  1217. }
  1218. status |= wait_many(FLAG_WAIT_ALL);
  1219. blkid_put_cache(cache);
  1220. return status;
  1221. }