blob: 3c6cafbd3f2b1ddee90e2306ef2d682fb872151d [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 **devices;
107static char **args;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000108static int num_devices;
109static int num_args;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000110static int verbose;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000111
112#define FS_TYPE_FLAG_NORMAL 0
113#define FS_TYPE_FLAG_OPT 1
114#define FS_TYPE_FLAG_NEGOPT 2
115static char **fs_type_list;
116static uint8_t *fs_type_flag;
117static smallint fs_type_negated;
118
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000119static volatile smallint cancel_requested;
120static smallint doall;
121static smallint noexecute;
122static smallint serialize;
123static smallint skip_root;
124/* static smallint like_mount; */
125static smallint notitle;
126static smallint parallel_root;
127static smallint force_all_parallel;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000128
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000129#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000130static smallint progress;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000131static int progress_fd;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000132#endif
133
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000134static int num_running;
135static int max_running;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000136static char *fstype;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000137static struct fs_info *filesys_info;
138static struct fs_info *filesys_last;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000139static struct fsck_instance *instance_list;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000140
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000141/*
142 * Return the "base device" given a particular device; this is used to
143 * assure that we only fsck one partition on a particular drive at any
144 * one time. Otherwise, the disk heads will be seeking all over the
145 * place. If the base device cannot be determined, return NULL.
146 *
147 * The base_device() function returns an allocated string which must
148 * be freed.
149 */
150#if ENABLE_FEATURE_DEVFS
151/*
152 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
153 * pathames.
154 */
155static const char *const devfs_hier[] = {
156 "host", "bus", "target", "lun", NULL
157};
158#endif
159
160static char *base_device(const char *device)
161{
162 char *str, *cp;
163#if ENABLE_FEATURE_DEVFS
164 const char *const *hier;
165 const char *disk;
166 int len;
167#endif
168 cp = str = xstrdup(device);
169
170 /* Skip over /dev/; if it's not present, give up. */
171 if (strncmp(cp, "/dev/", 5) != 0)
172 goto errout;
173 cp += 5;
174
175 /*
176 * For md devices, we treat them all as if they were all
177 * on one disk, since we don't know how to parallelize them.
178 */
179 if (cp[0] == 'm' && cp[1] == 'd') {
180 cp[2] = 0;
181 return str;
182 }
183
184 /* Handle DAC 960 devices */
185 if (strncmp(cp, "rd/", 3) == 0) {
186 cp += 3;
187 if (cp[0] != 'c' || !isdigit(cp[1])
188 || cp[2] != 'd' || !isdigit(cp[3]))
189 goto errout;
190 cp[4] = 0;
191 return str;
192 }
193
194 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
195 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
196 cp += 2;
197 /* If there's a single number after /dev/hd, skip it */
198 if (isdigit(*cp))
199 cp++;
200 /* What follows must be an alpha char, or give up */
201 if (!isalpha(*cp))
202 goto errout;
203 cp[1] = 0;
204 return str;
205 }
206
207#if ENABLE_FEATURE_DEVFS
208 /* Now let's handle devfs (ugh) names */
209 len = 0;
210 if (strncmp(cp, "ide/", 4) == 0)
211 len = 4;
212 if (strncmp(cp, "scsi/", 5) == 0)
213 len = 5;
214 if (len) {
215 cp += len;
216 /*
217 * Now we proceed down the expected devfs hierarchy.
218 * i.e., .../host1/bus2/target3/lun4/...
219 * If we don't find the expected token, followed by
220 * some number of digits at each level, abort.
221 */
222 for (hier = devfs_hier; *hier; hier++) {
223 len = strlen(*hier);
224 if (strncmp(cp, *hier, len) != 0)
225 goto errout;
226 cp += len;
227 while (*cp != '/' && *cp != 0) {
228 if (!isdigit(*cp))
229 goto errout;
230 cp++;
231 }
232 cp++;
233 }
234 cp[-1] = 0;
235 return str;
236 }
237
238 /* Now handle devfs /dev/disc or /dev/disk names */
239 disk = 0;
240 if (strncmp(cp, "discs/", 6) == 0)
241 disk = "disc";
242 else if (strncmp(cp, "disks/", 6) == 0)
243 disk = "disk";
244 if (disk) {
245 cp += 6;
246 if (strncmp(cp, disk, 4) != 0)
247 goto errout;
248 cp += 4;
249 while (*cp != '/' && *cp != 0) {
250 if (!isdigit(*cp))
251 goto errout;
252 cp++;
253 }
254 *cp = 0;
255 return str;
256 }
257#endif
258 errout:
259 free(str);
260 return NULL;
261}
262
263static void free_instance(struct fsck_instance *p)
264{
265 free(p->prog);
266 free(p->device);
267 free(p->base_device);
268 free(p);
269}
270
271static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
272 const char *type, const char *opts,
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000273 int passno)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000274{
275 struct fs_info *fs;
276
277 fs = xzalloc(sizeof(*fs));
278 fs->device = xstrdup(device);
279 fs->mountpt = xstrdup(mntpnt);
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000280 if (strchr(type, ','))
281 type = (char *)"auto";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000282 fs->type = xstrdup(type);
283 fs->opts = xstrdup(opts ? opts : "");
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000284 fs->passno = passno < 0 ? 1 : passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000285 /*fs->flags = 0; */
286 /*fs->next = NULL; */
287
288 if (!filesys_info)
289 filesys_info = fs;
290 else
291 filesys_last->next = fs;
292 filesys_last = fs;
293
294 return fs;
295}
296
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000297/* Load the filesystem database from /etc/fstab */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000298static void load_fs_info(const char *filename)
299{
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000300 FILE *fstab;
301 struct mntent mte;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000302 struct fs_info *fs;
303
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000304 fstab = setmntent(filename, "r");
305 if (!fstab) {
306 bb_perror_msg("cannot read %s", filename);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000307 return;
308 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000309
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000310 // Loop through entries
311 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
312 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000313 // mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000314 // mte.mnt_passno);
315 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000316 mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000317 mte.mnt_passno);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000318 }
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000319 endmntent(fstab);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000320}
321
322/* Lookup filesys in /etc/fstab and return the corresponding entry. */
323static struct fs_info *lookup(char *filesys)
324{
325 struct fs_info *fs;
326
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000327 for (fs = filesys_info; fs; fs = fs->next) {
328 if (strcmp(filesys, fs->device) == 0
329 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
330 )
331 break;
332 }
333
334 return fs;
335}
336
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000337#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000338static int progress_active(void)
339{
340 struct fsck_instance *inst;
341
342 for (inst = instance_list; inst; inst = inst->next) {
343 if (inst->flags & FLAG_DONE)
344 continue;
345 if (inst->flags & FLAG_PROGRESS)
346 return 1;
347 }
348 return 0;
349}
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000350#endif
351
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000352
353/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000354 * Send a signal to all outstanding fsck child processes
355 */
Denis Vlasenko0de93752006-12-26 02:51:29 +0000356static void kill_all_if_cancel_requested(void)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000357{
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000358 static smallint kill_sent;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000359
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000360 struct fsck_instance *inst;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000361
362 if (!cancel_requested || kill_sent)
363 return;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000364
365 for (inst = instance_list; inst; inst = inst->next) {
366 if (inst->flags & FLAG_DONE)
367 continue;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000368 kill(inst->pid, SIGTERM);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000369 }
Denis Vlasenko0de93752006-12-26 02:51:29 +0000370 kill_sent = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000371}
372
373/*
374 * Wait for one child process to exit; when it does, unlink it from
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000375 * the list of executing child processes, free, and return its exit status.
376 * If there is no exited child, return -1.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000377 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000378static int wait_one(int flags)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000379{
380 int status;
381 int sig;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000382 struct fsck_instance *inst, *prev;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000383 pid_t pid;
384
385 if (!instance_list)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000386 return -1;
387 /* if (noexecute) { already returned -1; } */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000388
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000389 while (1) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000390 pid = waitpid(-1, &status, flags);
Denis Vlasenko0de93752006-12-26 02:51:29 +0000391 kill_all_if_cancel_requested();
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000392 if (pid == 0) /* flags == WNOHANG and no children exited */
393 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000394 if (pid < 0) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000395 if (errno == EINTR)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000396 continue;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000397 if (errno == ECHILD) { /* paranoia */
398 bb_error_msg("wait: no more children");
399 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000400 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000401 bb_perror_msg("wait");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000402 continue;
403 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000404 prev = NULL;
405 inst = instance_list;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000406 do {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000407 if (inst->pid == pid)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000408 goto child_died;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000409 prev = inst;
410 inst = inst->next;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000411 } while (inst);
412 }
413 child_died:
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000414
415 if (WIFEXITED(status))
416 status = WEXITSTATUS(status);
417 else if (WIFSIGNALED(status)) {
418 sig = WTERMSIG(status);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000419 status = EXIT_UNCORRECTED;
420 if (sig != SIGINT) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000421 printf("Warning: %s %s terminated "
422 "by signal %d\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000423 inst->prog, inst->device, sig);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000424 status = EXIT_ERROR;
425 }
426 } else {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000427 printf("%s %s: status is %x, should never happen\n",
428 inst->prog, inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000429 status = EXIT_ERROR;
430 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000431
432#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000433 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000434 struct fsck_instance *inst2;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000435 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
436 if (inst2->flags & FLAG_DONE)
437 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000438 if (strcmp(inst2->type, "ext2") != 0
439 && strcmp(inst2->type, "ext3") != 0
440 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000441 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000442 }
443 /* ext[23], we will send USR1
444 * (request to start displaying progress bar)
445 *
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000446 * If we've just started the fsck, wait a tiny
447 * bit before sending the kill, to give it
448 * time to set up the signal handler
449 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000450 if (inst2->start_time >= time(NULL) - 1)
451 sleep(1);
452 kill(inst2->pid, SIGUSR1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000453 inst2->flags |= FLAG_PROGRESS;
454 break;
455 }
456 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000457#endif
458
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000459 if (prev)
460 prev->next = inst->next;
461 else
462 instance_list = inst->next;
463 if (verbose > 1)
464 printf("Finished with %s (exit status %d)\n",
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000465 inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000466 num_running--;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000467 free_instance(inst);
468
469 return status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000470}
471
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000472/*
473 * Wait until all executing child processes have exited; return the
474 * logical OR of all of their exit code values.
475 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000476#define FLAG_WAIT_ALL 0
477#define FLAG_WAIT_ATLEAST_ONE WNOHANG
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000478static int wait_many(int flags)
479{
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000480 int exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000481 int global_status = 0;
482 int wait_flags = 0;
483
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000484 while ((exit_status = wait_one(wait_flags)) != -1) {
485 global_status |= exit_status;
486 wait_flags |= flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000487 }
488 return global_status;
489}
490
491/*
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000492 * Execute a particular fsck program, and link it into the list of
493 * child processes we are waiting for.
494 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000495static void execute(const char *type, const char *device,
496 const char *mntpt /*, int interactive */)
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000497{
498 char *argv[num_args + 4]; /* see count below: */
499 int argc;
500 int i;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000501 struct fsck_instance *inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000502 pid_t pid;
503
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000504 argv[0] = xasprintf("fsck.%s", type); /* 1 */
505 for (i = 0; i < num_args; i++)
506 argv[i+1] = args[i]; /* num_args */
507 argc = num_args + 1;
508
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000509#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000510 if (progress && !progress_active()) {
511 if (strcmp(type, "ext2") == 0
512 || strcmp(type, "ext3") == 0
513 ) {
514 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
515 inst->flags |= FLAG_PROGRESS;
516 }
517 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000518#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000519
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000520 argv[argc++] = (char*)device; /* 1 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000521 argv[argc] = NULL; /* 1 */
522
523 if (verbose || noexecute) {
524 printf("[%s (%d) -- %s]", argv[0], num_running,
525 mntpt ? mntpt : device);
526 for (i = 0; i < argc; i++)
527 printf(" %s", argv[i]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000528 bb_putchar('\n');
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000529 }
530
531 /* Fork and execute the correct program. */
532 pid = -1;
533 if (!noexecute) {
Denis Vlasenko53091ec2007-03-26 13:35:09 +0000534 pid = spawn(argv);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000535 if (pid < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000536 bb_simple_perror_msg(argv[0]);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000537 }
538
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000539#if DO_PROGRESS_INDICATOR
540 free(argv[num_args + 1]);
541#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000542
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000543 /* No child, so don't record an instance */
544 if (pid <= 0) {
545 free(argv[0]);
Denis Vlasenko30eb3192008-02-02 18:54:58 +0000546 return;
547 }
548
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000549 inst = xzalloc(sizeof(*inst));
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000550 inst->pid = pid;
551 inst->prog = argv[0];
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000552 inst->device = xstrdup(device);
553 inst->base_device = base_device(device);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000554#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000555 inst->start_time = time(NULL);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000556#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000557
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000558 /* Add to the list of running fsck's.
559 * (was adding to the end, but adding to the front is simpler...) */
560 inst->next = instance_list;
561 instance_list = inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000562}
563
564/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000565 * Run the fsck program on a particular device
566 *
567 * If the type is specified using -t, and it isn't prefixed with "no"
568 * (as in "noext2") and only one filesystem type is specified, then
569 * use that type regardless of what is specified in /etc/fstab.
570 *
571 * If the type isn't specified by the user, then use either the type
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000572 * specified in /etc/fstab, or "auto".
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000573 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000574static void fsck_device(struct fs_info *fs /*, int interactive */)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000575{
576 const char *type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000577
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000578 if (strcmp(fs->type, "auto") != 0) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000579 type = fs->type;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000580 if (verbose > 2)
581 bb_info_msg("using filesystem type '%s' %s",
582 type, "from fstab");
583 } else if (fstype
Denis Vlasenko0de93752006-12-26 02:51:29 +0000584 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
585 && strncmp(fstype, "opts=", 5) != 0
586 && strncmp(fstype, "loop", 4) != 0
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000587 && !strchr(fstype, ',')
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000588 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000589 type = fstype;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000590 if (verbose > 2)
591 bb_info_msg("using filesystem type '%s' %s",
592 type, "from -t");
593 } else {
594 type = "auto";
595 if (verbose > 2)
596 bb_info_msg("using filesystem type '%s' %s",
597 type, "(default)");
598 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000599
600 num_running++;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000601 execute(type, fs->device, fs->mountpt /*, interactive */);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000602}
603
604/*
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000605 * Returns TRUE if a partition on the same disk is already being
606 * checked.
607 */
608static int device_already_active(char *device)
609{
610 struct fsck_instance *inst;
611 char *base;
612
613 if (force_all_parallel)
614 return 0;
615
616#ifdef BASE_MD
617 /* Don't check a soft raid disk with any other disk */
618 if (instance_list
619 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
620 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
621 ) {
622 return 1;
623 }
624#endif
625
626 base = base_device(device);
627 /*
628 * If we don't know the base device, assume that the device is
629 * already active if there are any fsck instances running.
630 */
631 if (!base)
632 return (instance_list != NULL);
633
634 for (inst = instance_list; inst; inst = inst->next) {
635 if (!inst->base_device || !strcmp(base, inst->base_device)) {
636 free(base);
637 return 1;
638 }
639 }
640
641 free(base);
642 return 0;
643}
644
645/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000646 * This function returns true if a particular option appears in a
647 * comma-delimited options list
648 */
649static int opt_in_list(char *opt, char *optlist)
650{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000651 char *s;
652 int len;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000653
654 if (!optlist)
655 return 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000656
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000657 len = strlen(opt);
658 s = optlist - 1;
659 while (1) {
660 s = strstr(s + 1, opt);
661 if (!s)
662 return 0;
663 /* neither "opt.." nor "xxx,opt.."? */
664 if (s != optlist && s[-1] != ',')
665 continue;
666 /* neither "..opt" nor "..opt,xxx"? */
667 if (s[len] != '\0' && s[len] != ',')
668 continue;
669 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000670 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000671}
672
673/* See if the filesystem matches the criteria given by the -t option */
674static int fs_match(struct fs_info *fs)
675{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000676 int n, ret, checked_type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000677 char *cp;
678
679 if (!fs_type_list)
680 return 1;
681
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000682 ret = 0;
683 checked_type = 0;
684 n = 0;
685 while (1) {
686 cp = fs_type_list[n];
687 if (!cp)
688 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000689 switch (fs_type_flag[n]) {
690 case FS_TYPE_FLAG_NORMAL:
691 checked_type++;
692 if (strcmp(cp, fs->type) == 0)
693 ret = 1;
694 break;
695 case FS_TYPE_FLAG_NEGOPT:
696 if (opt_in_list(cp, fs->opts))
697 return 0;
698 break;
699 case FS_TYPE_FLAG_OPT:
700 if (!opt_in_list(cp, fs->opts))
701 return 0;
702 break;
703 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000704 n++;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000705 }
706 if (checked_type == 0)
707 return 1;
708
709 return (fs_type_negated ? !ret : ret);
710}
711
712/* Check if we should ignore this filesystem. */
713static int ignore(struct fs_info *fs)
714{
715 /*
716 * If the pass number is 0, ignore it.
717 */
718 if (fs->passno == 0)
719 return 1;
720
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000721 /*
722 * If a specific fstype is specified, and it doesn't match,
723 * ignore it.
724 */
725 if (!fs_match(fs))
726 return 1;
727
728 /* Are we ignoring this type? */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000729 if (index_in_strings(ignored_types, fs->type) >= 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000730 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000731
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000732 /* We can and want to check this file system type. */
733 return 0;
734}
735
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000736/* Check all file systems, using the /etc/fstab table. */
737static int check_all(void)
738{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000739 struct fs_info *fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000740 int status = EXIT_OK;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000741 smallint not_done_yet;
742 smallint pass_done;
743 int passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000744
745 if (verbose)
746 puts("Checking all filesystems");
747
748 /*
749 * Do an initial scan over the filesystem; mark filesystems
750 * which should be ignored as done, and resolve any "auto"
751 * filesystem types (done as a side-effect of calling ignore()).
752 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000753 for (fs = filesys_info; fs; fs = fs->next)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000754 if (ignore(fs))
755 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000756
757 /*
758 * Find and check the root filesystem.
759 */
760 if (!parallel_root) {
761 for (fs = filesys_info; fs; fs = fs->next) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000762 if (LONE_CHAR(fs->mountpt, '/')) {
763 if (!skip_root && !ignore(fs)) {
764 fsck_device(fs /*, 1*/);
765 status |= wait_many(FLAG_WAIT_ALL);
766 if (status > EXIT_NONDESTRUCT)
767 return status;
768 }
769 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000770 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000771 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000772 }
773 }
774 /*
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000775 * This is for the bone-headed user who has root
776 * filesystem listed twice.
777 * "Skip root" will skip _all_ root entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000778 */
779 if (skip_root)
780 for (fs = filesys_info; fs; fs = fs->next)
781 if (LONE_CHAR(fs->mountpt, '/'))
782 fs->flags |= FLAG_DONE;
783
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000784 not_done_yet = 1;
785 passno = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000786 while (not_done_yet) {
787 not_done_yet = 0;
788 pass_done = 1;
789
790 for (fs = filesys_info; fs; fs = fs->next) {
791 if (cancel_requested)
792 break;
793 if (fs->flags & FLAG_DONE)
794 continue;
795 /*
796 * If the filesystem's pass number is higher
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000797 * than the current pass number, then we didn't
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000798 * do it yet.
799 */
800 if (fs->passno > passno) {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000801 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000802 continue;
803 }
804 /*
805 * If a filesystem on a particular device has
806 * already been spawned, then we need to defer
807 * this to another pass.
808 */
809 if (device_already_active(fs->device)) {
810 pass_done = 0;
811 continue;
812 }
813 /*
814 * Spawn off the fsck process
815 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000816 fsck_device(fs /*, serialize*/);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000817 fs->flags |= FLAG_DONE;
818
819 /*
820 * Only do one filesystem at a time, or if we
821 * have a limit on the number of fsck's extant
822 * at one time, apply that limit.
823 */
824 if (serialize
825 || (max_running && (num_running >= max_running))
826 ) {
827 pass_done = 0;
828 break;
829 }
830 }
831 if (cancel_requested)
832 break;
833 if (verbose > 1)
834 printf("--waiting-- (pass %d)\n", passno);
835 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
836 FLAG_WAIT_ATLEAST_ONE);
837 if (pass_done) {
838 if (verbose > 1)
839 puts("----------------------------------");
840 passno++;
841 } else
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000842 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000843 }
Denis Vlasenko0de93752006-12-26 02:51:29 +0000844 kill_all_if_cancel_requested();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000845 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
846 return status;
847}
848
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000849/*
850 * Deal with the fsck -t argument.
851 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
852 * Why here we require "-t novfat,nonfs" ??
853 */
854static void compile_fs_type(char *fs_type)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000855{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000856 char *s;
857 int num = 2;
858 smallint negate;
859
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000860 s = fs_type;
861 while ((s = strchr(s, ','))) {
862 num++;
863 s++;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000864 }
865
866 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
867 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
868 fs_type_negated = -1; /* not yet known is it negated or not */
869
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000870 num = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000871 s = fs_type;
872 while (1) {
873 char *comma;
874
875 negate = 0;
876 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
877 s += 2;
878 negate = 1;
879 } else if (s[0] == '!') {
880 s++;
881 negate = 1;
882 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000883
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000884 if (strcmp(s, "loop") == 0)
885 /* loop is really short-hand for opts=loop */
886 goto loop_special_case;
887 if (strncmp(s, "opts=", 5) == 0) {
888 s += 5;
889 loop_special_case:
890 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
891 } else {
892 if (fs_type_negated == -1)
893 fs_type_negated = negate;
894 if (fs_type_negated != negate)
895 bb_error_msg_and_die(
896"either all or none of the filesystem types passed to -t must be prefixed "
897"with 'no' or '!'");
898 }
899 comma = strchr(s, ',');
900 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
901 if (!comma)
902 break;
903 s = comma + 1;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000904 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000905}
906
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000907static void parse_args(char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000908{
909 int i, j;
910 char *arg, *tmp;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000911 char *options;
912 int optpos;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000913 int opts_for_fsck = 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000914
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000915 /* in bss, so already zeroed
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000916 num_devices = 0;
917 num_args = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000918 instance_list = NULL;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000919 */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000920
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000921 for (i = 1; argv[i]; i++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000922 arg = argv[i];
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000923
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000924 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000925 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000926// FIXME: must check that arg is a blkdev, or resolve
927// "/path", "UUID=xxx" or "LABEL=xxx" into block device name
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000928// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000929 devices = xrealloc_vector(devices, 2, num_devices);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000930 devices[num_devices++] = xstrdup(arg);
931 continue;
932 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000933
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000934 if (arg[0] != '-' || opts_for_fsck) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000935 args = xrealloc_vector(args, 2, num_args);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000936 args[num_args++] = xstrdup(arg);
937 continue;
938 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000939
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000940 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
941 opts_for_fsck = 1;
942 continue;
943 }
944
945 optpos = 0;
946 options = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000947 for (j = 1; arg[j]; j++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000948 switch (arg[j]) {
949 case 'A':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000950 doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000951 break;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000952#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000953 case 'C':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000954 progress = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000955 if (arg[++j]) { /* -Cn */
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000956 progress_fd = xatoi_u(&arg[j]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000957 goto next_arg;
958 }
959 /* -C n */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000960 if (!argv[++i]) bb_show_usage();
961 progress_fd = xatoi_u(argv[i]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000962 goto next_arg;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000963#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000964 case 'V':
965 verbose++;
966 break;
967 case 'N':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000968 noexecute = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000969 break;
970 case 'R':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000971 skip_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000972 break;
973 case 'T':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000974 notitle = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000975 break;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000976/* case 'M':
977 like_mount = 1;
978 break; */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000979 case 'P':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000980 parallel_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000981 break;
982 case 's':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000983 serialize = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000984 break;
985 case 't':
986 if (fstype)
987 bb_show_usage();
988 if (arg[++j])
989 tmp = &arg[j];
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000990 else if (argv[++i])
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000991 tmp = argv[i];
992 else
993 bb_show_usage();
994 fstype = xstrdup(tmp);
995 compile_fs_type(fstype);
996 goto next_arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000997 case '?':
998 bb_show_usage();
999 break;
1000 default:
1001 optpos++;
1002 /* one extra for '\0' */
1003 options = xrealloc(options, optpos + 2);
1004 options[optpos] = arg[j];
1005 break;
1006 }
1007 }
1008 next_arg:
1009 if (optpos) {
1010 options[0] = '-';
1011 options[optpos + 1] = '\0';
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001012 args = xrealloc_vector(args, 2, num_args);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001013 args[num_args++] = options;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001014 }
1015 }
1016 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001017 force_all_parallel = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001018 tmp = getenv("FSCK_MAX_INST");
1019 if (tmp)
1020 max_running = xatoi(tmp);
1021}
1022
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001023static void signal_cancel(int sig UNUSED_PARAM)
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001024{
1025 cancel_requested = 1;
1026}
1027
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001028int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001029int fsck_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001030{
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001031 int i, status;
1032 /*int interactive;*/
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001033 const char *fstab;
1034 struct fs_info *fs;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001035
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001036 /* we want wait() to be interruptible */
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00001037 signal_no_SA_RESTART_empty_mask(SIGINT, signal_cancel);
1038 signal_no_SA_RESTART_empty_mask(SIGTERM, signal_cancel);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001039
1040 setbuf(stdout, NULL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001041
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001042 parse_args(argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001043
1044 if (!notitle)
1045 puts("fsck (busybox "BB_VER", "BB_BT")");
1046
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001047 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1048 * so we are scanning it anyway */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001049 fstab = getenv("FSTAB_FILE");
1050 if (!fstab)
1051 fstab = "/etc/fstab";
1052 load_fs_info(fstab);
1053
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001054 /*interactive = (num_devices == 1) | serialize;*/
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001055
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001056 if (num_devices == 0)
1057 /*interactive =*/ serialize = doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001058 if (doall)
1059 return check_all();
1060
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001061 status = 0;
Denis Vlasenko0de93752006-12-26 02:51:29 +00001062 for (i = 0; i < num_devices; i++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001063 if (cancel_requested) {
Denis Vlasenko0de93752006-12-26 02:51:29 +00001064 kill_all_if_cancel_requested();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001065 break;
1066 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001067
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001068 fs = lookup(devices[i]);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001069 if (!fs)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001070 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1071 fsck_device(fs /*, interactive */);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001072
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001073 if (serialize
1074 || (max_running && (num_running >= max_running))
1075 ) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001076 int exit_status = wait_one(0);
1077 if (exit_status >= 0)
1078 status |= exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001079 if (verbose > 1)
1080 puts("----------------------------------");
1081 }
1082 }
1083 status |= wait_many(FLAG_WAIT_ALL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001084 return status;
1085}