blob: 21b2fdc0d861f562d632e7d9851095d1f9408721 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * fsck --- 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 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +000023 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000024 */
25
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000026/* All filesystem specific hooks have been removed.
27 * If filesystem cannot be determined, we will execute
28 * "fsck.auto". Currently this also happens if you specify
29 * UUID=xxx or LABEL=xxx as an object to check.
30 * Detection code for that is also probably has to be in fsck.auto.
31 *
32 * In other words, this is _really_ is just a driver program which
33 * spawns actual fsck.something for each filesystem to check.
34 * It doesn't guess filesystem types from on-disk format.
35 */
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000038
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000039/* "progress indicator" code is somewhat buggy and ext[23] specific.
40 * We should be filesystem agnostic. IOW: there should be a well-defined
41 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
42#define DO_PROGRESS_INDICATOR 0
43
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000044#define EXIT_OK 0
45#define EXIT_NONDESTRUCT 1
46#define EXIT_DESTRUCT 2
47#define EXIT_UNCORRECTED 4
48#define EXIT_ERROR 8
49#define EXIT_USAGE 16
50#define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
51
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000052/*
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000053 * Internal structure for mount table entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000054 */
55
56struct fs_info {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000057 struct fs_info *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000058 char *device;
59 char *mountpt;
60 char *type;
61 char *opts;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000062 int passno;
63 int flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000064};
65
66#define FLAG_DONE 1
67#define FLAG_PROGRESS 2
68/*
69 * Structure to allow exit codes to be stored
70 */
71struct fsck_instance {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000072 struct fsck_instance *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000073 int pid;
74 int flags;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000075#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000076 time_t start_time;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000077#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000078 char *prog;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000079 char *device;
80 char *base_device; /* /dev/hda for /dev/hdaN etc */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000081};
82
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000083static const char ignored_types[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000084 "ignore\0"
85 "iso9660\0"
86 "nfs\0"
87 "proc\0"
88 "sw\0"
89 "swap\0"
90 "tmpfs\0"
91 "devpts\0";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000092
Denis Vlasenko0de93752006-12-26 02:51:29 +000093#if 0
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000094static const char really_wanted[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000095 "minix\0"
96 "ext2\0"
97 "ext3\0"
98 "jfs\0"
99 "reiserfs\0"
100 "xiafs\0"
101 "xfs\0";
Denis Vlasenko0de93752006-12-26 02:51:29 +0000102#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000103
104#define BASE_MD "/dev/md"
105
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000106static char **args;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000107static int num_args;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000108static int verbose;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000109
110#define FS_TYPE_FLAG_NORMAL 0
111#define FS_TYPE_FLAG_OPT 1
112#define FS_TYPE_FLAG_NEGOPT 2
113static char **fs_type_list;
114static uint8_t *fs_type_flag;
115static smallint fs_type_negated;
116
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000117static smallint noexecute;
118static smallint serialize;
119static smallint skip_root;
120/* static smallint like_mount; */
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000121static smallint parallel_root;
122static smallint force_all_parallel;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000123
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000124#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000125static smallint progress;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000126static int progress_fd;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000127#endif
128
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000129static int num_running;
130static int max_running;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000131static char *fstype;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000132static struct fs_info *filesys_info;
133static struct fs_info *filesys_last;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000134static struct fsck_instance *instance_list;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000135
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000136/*
137 * Return the "base device" given a particular device; this is used to
138 * assure that we only fsck one partition on a particular drive at any
139 * one time. Otherwise, the disk heads will be seeking all over the
140 * place. If the base device cannot be determined, return NULL.
141 *
142 * The base_device() function returns an allocated string which must
143 * be freed.
144 */
145#if ENABLE_FEATURE_DEVFS
146/*
147 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
148 * pathames.
149 */
150static const char *const devfs_hier[] = {
151 "host", "bus", "target", "lun", NULL
152};
153#endif
154
155static char *base_device(const char *device)
156{
157 char *str, *cp;
158#if ENABLE_FEATURE_DEVFS
159 const char *const *hier;
160 const char *disk;
161 int len;
162#endif
163 cp = str = xstrdup(device);
164
165 /* Skip over /dev/; if it's not present, give up. */
166 if (strncmp(cp, "/dev/", 5) != 0)
167 goto errout;
168 cp += 5;
169
170 /*
171 * For md devices, we treat them all as if they were all
172 * on one disk, since we don't know how to parallelize them.
173 */
174 if (cp[0] == 'm' && cp[1] == 'd') {
175 cp[2] = 0;
176 return str;
177 }
178
179 /* Handle DAC 960 devices */
180 if (strncmp(cp, "rd/", 3) == 0) {
181 cp += 3;
182 if (cp[0] != 'c' || !isdigit(cp[1])
183 || cp[2] != 'd' || !isdigit(cp[3]))
184 goto errout;
185 cp[4] = 0;
186 return str;
187 }
188
189 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
190 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
191 cp += 2;
192 /* If there's a single number after /dev/hd, skip it */
193 if (isdigit(*cp))
194 cp++;
195 /* What follows must be an alpha char, or give up */
196 if (!isalpha(*cp))
197 goto errout;
198 cp[1] = 0;
199 return str;
200 }
201
202#if ENABLE_FEATURE_DEVFS
203 /* Now let's handle devfs (ugh) names */
204 len = 0;
205 if (strncmp(cp, "ide/", 4) == 0)
206 len = 4;
207 if (strncmp(cp, "scsi/", 5) == 0)
208 len = 5;
209 if (len) {
210 cp += len;
211 /*
212 * Now we proceed down the expected devfs hierarchy.
213 * i.e., .../host1/bus2/target3/lun4/...
214 * If we don't find the expected token, followed by
215 * some number of digits at each level, abort.
216 */
217 for (hier = devfs_hier; *hier; hier++) {
218 len = strlen(*hier);
219 if (strncmp(cp, *hier, len) != 0)
220 goto errout;
221 cp += len;
222 while (*cp != '/' && *cp != 0) {
223 if (!isdigit(*cp))
224 goto errout;
225 cp++;
226 }
227 cp++;
228 }
229 cp[-1] = 0;
230 return str;
231 }
232
233 /* Now handle devfs /dev/disc or /dev/disk names */
234 disk = 0;
235 if (strncmp(cp, "discs/", 6) == 0)
236 disk = "disc";
237 else if (strncmp(cp, "disks/", 6) == 0)
238 disk = "disk";
239 if (disk) {
240 cp += 6;
241 if (strncmp(cp, disk, 4) != 0)
242 goto errout;
243 cp += 4;
244 while (*cp != '/' && *cp != 0) {
245 if (!isdigit(*cp))
246 goto errout;
247 cp++;
248 }
249 *cp = 0;
250 return str;
251 }
252#endif
253 errout:
254 free(str);
255 return NULL;
256}
257
258static void free_instance(struct fsck_instance *p)
259{
260 free(p->prog);
261 free(p->device);
262 free(p->base_device);
263 free(p);
264}
265
266static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
267 const char *type, const char *opts,
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000268 int passno)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000269{
270 struct fs_info *fs;
271
272 fs = xzalloc(sizeof(*fs));
273 fs->device = xstrdup(device);
274 fs->mountpt = xstrdup(mntpnt);
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000275 if (strchr(type, ','))
276 type = (char *)"auto";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000277 fs->type = xstrdup(type);
278 fs->opts = xstrdup(opts ? opts : "");
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000279 fs->passno = passno < 0 ? 1 : passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000280 /*fs->flags = 0; */
281 /*fs->next = NULL; */
282
283 if (!filesys_info)
284 filesys_info = fs;
285 else
286 filesys_last->next = fs;
287 filesys_last = fs;
288
289 return fs;
290}
291
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000292/* Load the filesystem database from /etc/fstab */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000293static void load_fs_info(const char *filename)
294{
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000295 FILE *fstab;
296 struct mntent mte;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000297 struct fs_info *fs;
298
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000299 fstab = setmntent(filename, "r");
300 if (!fstab) {
301 bb_perror_msg("cannot read %s", filename);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000302 return;
303 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000304
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000305 // Loop through entries
306 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
307 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000308 // mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000309 // mte.mnt_passno);
310 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000311 mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000312 mte.mnt_passno);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000313 }
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000314 endmntent(fstab);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000315}
316
317/* Lookup filesys in /etc/fstab and return the corresponding entry. */
318static struct fs_info *lookup(char *filesys)
319{
320 struct fs_info *fs;
321
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000322 for (fs = filesys_info; fs; fs = fs->next) {
323 if (strcmp(filesys, fs->device) == 0
324 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
325 )
326 break;
327 }
328
329 return fs;
330}
331
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000332#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000333static int progress_active(void)
334{
335 struct fsck_instance *inst;
336
337 for (inst = instance_list; inst; inst = inst->next) {
338 if (inst->flags & FLAG_DONE)
339 continue;
340 if (inst->flags & FLAG_PROGRESS)
341 return 1;
342 }
343 return 0;
344}
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000345#endif
346
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000347
348/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000349 * Send a signal to all outstanding fsck child processes
350 */
Denys Vlasenko66837452009-09-13 05:52:03 +0200351static void kill_all_if_got_signal(void)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000352{
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000353 static smallint kill_sent;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000354
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000355 struct fsck_instance *inst;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000356
Denys Vlasenko66837452009-09-13 05:52:03 +0200357 if (!bb_got_signal || kill_sent)
Denis Vlasenko0de93752006-12-26 02:51:29 +0000358 return;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000359
360 for (inst = instance_list; inst; inst = inst->next) {
361 if (inst->flags & FLAG_DONE)
362 continue;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000363 kill(inst->pid, SIGTERM);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000364 }
Denis Vlasenko0de93752006-12-26 02:51:29 +0000365 kill_sent = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000366}
367
368/*
369 * Wait for one child process to exit; when it does, unlink it from
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000370 * the list of executing child processes, free, and return its exit status.
371 * If there is no exited child, return -1.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000372 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000373static int wait_one(int flags)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000374{
375 int status;
376 int sig;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000377 struct fsck_instance *inst, *prev;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000378 pid_t pid;
379
380 if (!instance_list)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000381 return -1;
382 /* if (noexecute) { already returned -1; } */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000383
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000384 while (1) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000385 pid = waitpid(-1, &status, flags);
Denys Vlasenko66837452009-09-13 05:52:03 +0200386 kill_all_if_got_signal();
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000387 if (pid == 0) /* flags == WNOHANG and no children exited */
388 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000389 if (pid < 0) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000390 if (errno == EINTR)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000391 continue;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000392 if (errno == ECHILD) { /* paranoia */
393 bb_error_msg("wait: no more children");
394 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000395 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000396 bb_perror_msg("wait");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000397 continue;
398 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000399 prev = NULL;
400 inst = instance_list;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000401 do {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000402 if (inst->pid == pid)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000403 goto child_died;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000404 prev = inst;
405 inst = inst->next;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000406 } while (inst);
407 }
408 child_died:
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000409
410 if (WIFEXITED(status))
411 status = WEXITSTATUS(status);
412 else if (WIFSIGNALED(status)) {
413 sig = WTERMSIG(status);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000414 status = EXIT_UNCORRECTED;
415 if (sig != SIGINT) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000416 printf("Warning: %s %s terminated "
417 "by signal %d\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000418 inst->prog, inst->device, sig);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000419 status = EXIT_ERROR;
420 }
421 } else {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000422 printf("%s %s: status is %x, should never happen\n",
423 inst->prog, inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000424 status = EXIT_ERROR;
425 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000426
427#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000428 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000429 struct fsck_instance *inst2;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000430 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
431 if (inst2->flags & FLAG_DONE)
432 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000433 if (strcmp(inst2->type, "ext2") != 0
434 && strcmp(inst2->type, "ext3") != 0
435 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000436 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000437 }
438 /* ext[23], we will send USR1
439 * (request to start displaying progress bar)
440 *
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000441 * If we've just started the fsck, wait a tiny
442 * bit before sending the kill, to give it
443 * time to set up the signal handler
444 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000445 if (inst2->start_time >= time(NULL) - 1)
446 sleep(1);
447 kill(inst2->pid, SIGUSR1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000448 inst2->flags |= FLAG_PROGRESS;
449 break;
450 }
451 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000452#endif
453
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000454 if (prev)
455 prev->next = inst->next;
456 else
457 instance_list = inst->next;
458 if (verbose > 1)
459 printf("Finished with %s (exit status %d)\n",
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000460 inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000461 num_running--;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000462 free_instance(inst);
463
464 return status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000465}
466
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000467/*
468 * Wait until all executing child processes have exited; return the
469 * logical OR of all of their exit code values.
470 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000471#define FLAG_WAIT_ALL 0
472#define FLAG_WAIT_ATLEAST_ONE WNOHANG
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000473static int wait_many(int flags)
474{
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000475 int exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000476 int global_status = 0;
477 int wait_flags = 0;
478
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000479 while ((exit_status = wait_one(wait_flags)) != -1) {
480 global_status |= exit_status;
481 wait_flags |= flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000482 }
483 return global_status;
484}
485
486/*
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000487 * Execute a particular fsck program, and link it into the list of
488 * child processes we are waiting for.
489 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000490static void execute(const char *type, const char *device,
491 const char *mntpt /*, int interactive */)
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000492{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000493 int i;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000494 struct fsck_instance *inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000495 pid_t pid;
496
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200497 args[0] = xasprintf("fsck.%s", type);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000498
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000499#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000500 if (progress && !progress_active()) {
501 if (strcmp(type, "ext2") == 0
502 || strcmp(type, "ext3") == 0
503 ) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200504 args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000505 inst->flags |= FLAG_PROGRESS;
506 }
507 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000508#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000509
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200510 args[num_args - 2] = (char*)device;
511 /* args[num_args - 1] = NULL; - already is */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000512
513 if (verbose || noexecute) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200514 printf("[%s (%d) -- %s]", args[0], num_running,
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000515 mntpt ? mntpt : device);
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200516 for (i = 0; args[i]; i++)
517 printf(" %s", args[i]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000518 bb_putchar('\n');
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000519 }
520
521 /* Fork and execute the correct program. */
522 pid = -1;
523 if (!noexecute) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200524 pid = spawn(args);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000525 if (pid < 0)
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200526 bb_simple_perror_msg(args[0]);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000527 }
528
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000529#if DO_PROGRESS_INDICATOR
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200530 free(args[XXX]);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000531#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000532
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000533 /* No child, so don't record an instance */
534 if (pid <= 0) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200535 free(args[0]);
Denis Vlasenko30eb3192008-02-02 18:54:58 +0000536 return;
537 }
538
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000539 inst = xzalloc(sizeof(*inst));
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000540 inst->pid = pid;
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200541 inst->prog = args[0];
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000542 inst->device = xstrdup(device);
543 inst->base_device = base_device(device);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000544#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000545 inst->start_time = time(NULL);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000546#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000547
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000548 /* Add to the list of running fsck's.
549 * (was adding to the end, but adding to the front is simpler...) */
550 inst->next = instance_list;
551 instance_list = inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000552}
553
554/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000555 * Run the fsck program on a particular device
556 *
557 * If the type is specified using -t, and it isn't prefixed with "no"
558 * (as in "noext2") and only one filesystem type is specified, then
559 * use that type regardless of what is specified in /etc/fstab.
560 *
561 * If the type isn't specified by the user, then use either the type
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000562 * specified in /etc/fstab, or "auto".
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000563 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000564static void fsck_device(struct fs_info *fs /*, int interactive */)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000565{
566 const char *type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000567
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000568 if (strcmp(fs->type, "auto") != 0) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000569 type = fs->type;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000570 if (verbose > 2)
571 bb_info_msg("using filesystem type '%s' %s",
572 type, "from fstab");
573 } else if (fstype
Denis Vlasenko0de93752006-12-26 02:51:29 +0000574 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
575 && strncmp(fstype, "opts=", 5) != 0
576 && strncmp(fstype, "loop", 4) != 0
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000577 && !strchr(fstype, ',')
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000578 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000579 type = fstype;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000580 if (verbose > 2)
581 bb_info_msg("using filesystem type '%s' %s",
582 type, "from -t");
583 } else {
584 type = "auto";
585 if (verbose > 2)
586 bb_info_msg("using filesystem type '%s' %s",
587 type, "(default)");
588 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000589
590 num_running++;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000591 execute(type, fs->device, fs->mountpt /*, interactive */);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000592}
593
594/*
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000595 * Returns TRUE if a partition on the same disk is already being
596 * checked.
597 */
598static int device_already_active(char *device)
599{
600 struct fsck_instance *inst;
601 char *base;
602
603 if (force_all_parallel)
604 return 0;
605
606#ifdef BASE_MD
607 /* Don't check a soft raid disk with any other disk */
608 if (instance_list
609 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
610 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
611 ) {
612 return 1;
613 }
614#endif
615
616 base = base_device(device);
617 /*
618 * If we don't know the base device, assume that the device is
619 * already active if there are any fsck instances running.
620 */
621 if (!base)
622 return (instance_list != NULL);
623
624 for (inst = instance_list; inst; inst = inst->next) {
625 if (!inst->base_device || !strcmp(base, inst->base_device)) {
626 free(base);
627 return 1;
628 }
629 }
630
631 free(base);
632 return 0;
633}
634
635/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000636 * This function returns true if a particular option appears in a
637 * comma-delimited options list
638 */
639static int opt_in_list(char *opt, char *optlist)
640{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000641 char *s;
642 int len;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000643
644 if (!optlist)
645 return 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000646
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000647 len = strlen(opt);
648 s = optlist - 1;
649 while (1) {
650 s = strstr(s + 1, opt);
651 if (!s)
652 return 0;
653 /* neither "opt.." nor "xxx,opt.."? */
654 if (s != optlist && s[-1] != ',')
655 continue;
656 /* neither "..opt" nor "..opt,xxx"? */
657 if (s[len] != '\0' && s[len] != ',')
658 continue;
659 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000660 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000661}
662
663/* See if the filesystem matches the criteria given by the -t option */
664static int fs_match(struct fs_info *fs)
665{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000666 int n, ret, checked_type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000667 char *cp;
668
669 if (!fs_type_list)
670 return 1;
671
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000672 ret = 0;
673 checked_type = 0;
674 n = 0;
675 while (1) {
676 cp = fs_type_list[n];
677 if (!cp)
678 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000679 switch (fs_type_flag[n]) {
680 case FS_TYPE_FLAG_NORMAL:
681 checked_type++;
682 if (strcmp(cp, fs->type) == 0)
683 ret = 1;
684 break;
685 case FS_TYPE_FLAG_NEGOPT:
686 if (opt_in_list(cp, fs->opts))
687 return 0;
688 break;
689 case FS_TYPE_FLAG_OPT:
690 if (!opt_in_list(cp, fs->opts))
691 return 0;
692 break;
693 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000694 n++;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000695 }
696 if (checked_type == 0)
697 return 1;
698
699 return (fs_type_negated ? !ret : ret);
700}
701
702/* Check if we should ignore this filesystem. */
703static int ignore(struct fs_info *fs)
704{
705 /*
706 * If the pass number is 0, ignore it.
707 */
708 if (fs->passno == 0)
709 return 1;
710
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000711 /*
712 * If a specific fstype is specified, and it doesn't match,
713 * ignore it.
714 */
715 if (!fs_match(fs))
716 return 1;
717
718 /* Are we ignoring this type? */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000719 if (index_in_strings(ignored_types, fs->type) >= 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000720 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000721
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000722 /* We can and want to check this file system type. */
723 return 0;
724}
725
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000726/* Check all file systems, using the /etc/fstab table. */
727static int check_all(void)
728{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000729 struct fs_info *fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000730 int status = EXIT_OK;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000731 smallint not_done_yet;
732 smallint pass_done;
733 int passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000734
735 if (verbose)
736 puts("Checking all filesystems");
737
738 /*
739 * Do an initial scan over the filesystem; mark filesystems
740 * which should be ignored as done, and resolve any "auto"
741 * filesystem types (done as a side-effect of calling ignore()).
742 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000743 for (fs = filesys_info; fs; fs = fs->next)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000744 if (ignore(fs))
745 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000746
747 /*
748 * Find and check the root filesystem.
749 */
750 if (!parallel_root) {
751 for (fs = filesys_info; fs; fs = fs->next) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000752 if (LONE_CHAR(fs->mountpt, '/')) {
753 if (!skip_root && !ignore(fs)) {
754 fsck_device(fs /*, 1*/);
755 status |= wait_many(FLAG_WAIT_ALL);
756 if (status > EXIT_NONDESTRUCT)
757 return status;
758 }
759 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000760 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000761 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000762 }
763 }
764 /*
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000765 * This is for the bone-headed user who has root
766 * filesystem listed twice.
767 * "Skip root" will skip _all_ root entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000768 */
769 if (skip_root)
770 for (fs = filesys_info; fs; fs = fs->next)
771 if (LONE_CHAR(fs->mountpt, '/'))
772 fs->flags |= FLAG_DONE;
773
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000774 not_done_yet = 1;
775 passno = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000776 while (not_done_yet) {
777 not_done_yet = 0;
778 pass_done = 1;
779
780 for (fs = filesys_info; fs; fs = fs->next) {
Denys Vlasenko66837452009-09-13 05:52:03 +0200781 if (bb_got_signal)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000782 break;
783 if (fs->flags & FLAG_DONE)
784 continue;
785 /*
786 * If the filesystem's pass number is higher
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000787 * than the current pass number, then we didn't
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000788 * do it yet.
789 */
790 if (fs->passno > passno) {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000791 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000792 continue;
793 }
794 /*
795 * If a filesystem on a particular device has
796 * already been spawned, then we need to defer
797 * this to another pass.
798 */
799 if (device_already_active(fs->device)) {
800 pass_done = 0;
801 continue;
802 }
803 /*
804 * Spawn off the fsck process
805 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000806 fsck_device(fs /*, serialize*/);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000807 fs->flags |= FLAG_DONE;
808
809 /*
810 * Only do one filesystem at a time, or if we
811 * have a limit on the number of fsck's extant
812 * at one time, apply that limit.
813 */
814 if (serialize
815 || (max_running && (num_running >= max_running))
816 ) {
817 pass_done = 0;
818 break;
819 }
820 }
Denys Vlasenko66837452009-09-13 05:52:03 +0200821 if (bb_got_signal)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000822 break;
823 if (verbose > 1)
824 printf("--waiting-- (pass %d)\n", passno);
825 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
826 FLAG_WAIT_ATLEAST_ONE);
827 if (pass_done) {
828 if (verbose > 1)
829 puts("----------------------------------");
830 passno++;
831 } else
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000832 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000833 }
Denys Vlasenko66837452009-09-13 05:52:03 +0200834 kill_all_if_got_signal();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000835 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
836 return status;
837}
838
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000839/*
840 * Deal with the fsck -t argument.
841 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
842 * Why here we require "-t novfat,nonfs" ??
843 */
844static void compile_fs_type(char *fs_type)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000845{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000846 char *s;
847 int num = 2;
848 smallint negate;
849
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000850 s = fs_type;
851 while ((s = strchr(s, ','))) {
852 num++;
853 s++;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000854 }
855
856 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
857 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
858 fs_type_negated = -1; /* not yet known is it negated or not */
859
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000860 num = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000861 s = fs_type;
862 while (1) {
863 char *comma;
864
865 negate = 0;
866 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
867 s += 2;
868 negate = 1;
869 } else if (s[0] == '!') {
870 s++;
871 negate = 1;
872 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000873
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000874 if (strcmp(s, "loop") == 0)
875 /* loop is really short-hand for opts=loop */
876 goto loop_special_case;
877 if (strncmp(s, "opts=", 5) == 0) {
878 s += 5;
879 loop_special_case:
880 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
881 } else {
882 if (fs_type_negated == -1)
883 fs_type_negated = negate;
884 if (fs_type_negated != negate)
885 bb_error_msg_and_die(
886"either all or none of the filesystem types passed to -t must be prefixed "
887"with 'no' or '!'");
888 }
889 comma = strchr(s, ',');
890 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
891 if (!comma)
892 break;
893 s = comma + 1;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000894 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000895}
896
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200897static char **new_args(void)
898{
899 args = xrealloc_vector(args, 2, num_args);
900 return &args[num_args++];
901}
902
Denys Vlasenko66837452009-09-13 05:52:03 +0200903int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
904int fsck_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000905{
Denys Vlasenko66837452009-09-13 05:52:03 +0200906 int i, status;
907 /*int interactive;*/
908 struct fs_info *fs;
909 const char *fstab;
910 char *tmp;
911 char **devices;
912 int num_devices;
913 smallint opts_for_fsck;
914 smallint doall;
915 smallint notitle;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000916
Denys Vlasenko66837452009-09-13 05:52:03 +0200917 /* we want wait() to be interruptible */
918 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
919 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
920
921 setbuf(stdout, NULL);
922
923 opts_for_fsck = doall = notitle = 0;
924 devices = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000925 num_devices = 0;
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200926 new_args(); /* args[0] = NULL, will be replaced by fsck.<type> */
927 /* instance_list = NULL; - in bss, so already zeroed */
928
Denys Vlasenko66837452009-09-13 05:52:03 +0200929 while (*++argv) {
930 int j;
931 int optpos;
932 char *options;
933 char *arg = *argv;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000934
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000935 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000936 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000937// FIXME: must check that arg is a blkdev, or resolve
938// "/path", "UUID=xxx" or "LABEL=xxx" into block device name
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000939// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000940 devices = xrealloc_vector(devices, 2, num_devices);
Denys Vlasenko66837452009-09-13 05:52:03 +0200941 devices[num_devices++] = arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000942 continue;
943 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000944
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000945 if (arg[0] != '-' || opts_for_fsck) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200946 *new_args() = arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000947 continue;
948 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000949
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000950 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
951 opts_for_fsck = 1;
952 continue;
953 }
954
955 optpos = 0;
956 options = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000957 for (j = 1; arg[j]; j++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000958 switch (arg[j]) {
959 case 'A':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000960 doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000961 break;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000962#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000963 case 'C':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000964 progress = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000965 if (arg[++j]) { /* -Cn */
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000966 progress_fd = xatoi_u(&arg[j]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000967 goto next_arg;
968 }
969 /* -C n */
Denys Vlasenko66837452009-09-13 05:52:03 +0200970 if (!*++argv)
971 bb_show_usage();
972 progress_fd = xatoi_u(*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000973 goto next_arg;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000974#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000975 case 'V':
976 verbose++;
977 break;
978 case 'N':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000979 noexecute = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000980 break;
981 case 'R':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000982 skip_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000983 break;
984 case 'T':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000985 notitle = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000986 break;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000987/* case 'M':
988 like_mount = 1;
989 break; */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000990 case 'P':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000991 parallel_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000992 break;
993 case 's':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000994 serialize = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000995 break;
996 case 't':
997 if (fstype)
998 bb_show_usage();
999 if (arg[++j])
1000 tmp = &arg[j];
Denys Vlasenko66837452009-09-13 05:52:03 +02001001 else if (*++argv)
1002 tmp = *argv;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001003 else
1004 bb_show_usage();
1005 fstype = xstrdup(tmp);
1006 compile_fs_type(fstype);
1007 goto next_arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001008 case '?':
1009 bb_show_usage();
1010 break;
1011 default:
1012 optpos++;
1013 /* one extra for '\0' */
1014 options = xrealloc(options, optpos + 2);
1015 options[optpos] = arg[j];
1016 break;
1017 }
1018 }
1019 next_arg:
1020 if (optpos) {
1021 options[0] = '-';
1022 options[optpos + 1] = '\0';
Denys Vlasenko7649bd02009-09-13 05:52:46 +02001023 *new_args() = options;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001024 }
1025 }
1026 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001027 force_all_parallel = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001028 tmp = getenv("FSCK_MAX_INST");
1029 if (tmp)
1030 max_running = xatoi(tmp);
Denys Vlasenko7649bd02009-09-13 05:52:46 +02001031 new_args(); /* args[num_args - 2] will be replaced by <device> */
1032 new_args(); /* args[num_args - 1] is the last, NULL element */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001033
1034 if (!notitle)
1035 puts("fsck (busybox "BB_VER", "BB_BT")");
1036
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001037 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1038 * so we are scanning it anyway */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001039 fstab = getenv("FSTAB_FILE");
1040 if (!fstab)
1041 fstab = "/etc/fstab";
1042 load_fs_info(fstab);
1043
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001044 /*interactive = (num_devices == 1) | serialize;*/
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001045
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001046 if (num_devices == 0)
1047 /*interactive =*/ serialize = doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001048 if (doall)
1049 return check_all();
1050
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001051 status = 0;
Denis Vlasenko0de93752006-12-26 02:51:29 +00001052 for (i = 0; i < num_devices; i++) {
Denys Vlasenko66837452009-09-13 05:52:03 +02001053 if (bb_got_signal) {
1054 kill_all_if_got_signal();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001055 break;
1056 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001057
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001058 fs = lookup(devices[i]);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001059 if (!fs)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001060 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1061 fsck_device(fs /*, interactive */);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001062
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001063 if (serialize
1064 || (max_running && (num_running >= max_running))
1065 ) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001066 int exit_status = wait_one(0);
1067 if (exit_status >= 0)
1068 status |= exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001069 if (verbose > 1)
1070 puts("----------------------------------");
1071 }
1072 }
1073 status |= wait_many(FLAG_WAIT_ALL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001074 return status;
1075}