+/* vi: set sw=4 ts=4: */
/*
* pfsck --- A generic, parallelizing front-end for the fsck program.
* It will automatically try to run fsck programs in parallel if the
#include <sys/types.h>
#include <sys/wait.h>
-#include <sys/signal.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdio.h>
#include <paths.h>
#include <unistd.h>
#include <errno.h>
-#include <malloc.h>
#include <signal.h>
#include "fsck.h"
#include "e2fsbb.h"
+#include "busybox.h"
+
#ifndef _PATH_MNTTAB
#define _PATH_MNTTAB "/etc/fstab"
#endif
+/*
+ * fsck.h
+ */
+
+#ifndef DEFAULT_FSTYPE
+#define DEFAULT_FSTYPE "ext2"
+#endif
+
+#define MAX_DEVICES 32
+#define MAX_ARGS 32
+
+/*
+ * Internal structure for mount tabel entries.
+ */
+
+struct fs_info {
+ char *device;
+ char *mountpt;
+ char *type;
+ char *opts;
+ int freq;
+ int passno;
+ int flags;
+ struct fs_info *next;
+};
+
+#define FLAG_DONE 1
+#define FLAG_PROGRESS 2
+
+/*
+ * Structure to allow exit codes to be stored
+ */
+struct fsck_instance {
+ int pid;
+ int flags;
+ int exit_status;
+ time_t start_time;
+ char * prog;
+ char * type;
+ char * device;
+ char * base_device;
+ struct fsck_instance *next;
+};
+
+/*
+ * base_device.c
+ *
+ * Return the "base device" given a particular device; this is used to
+ * assure that we only fsck one partition on a particular drive at any
+ * one time. Otherwise, the disk heads will be seeking all over the
+ * place. If the base device cannot be determined, return NULL.
+ *
+ * The base_device() function returns an allocated string which must
+ * be freed.
+ *
+ */
+
+
+#ifdef CONFIG_FEATURE_DEVFS
+/*
+ * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
+ * pathames.
+ */
+static const char * const devfs_hier[] = {
+ "host", "bus", "target", "lun", 0
+};
+#endif
+
+static char *base_device(const char *device)
+{
+ char *str, *cp;
+#ifdef CONFIG_FEATURE_DEVFS
+ const char * const *hier;
+ const char *disk;
+ int len;
+#endif
+
+ cp = str = xstrdup(device);
+
+ /* Skip over /dev/; if it's not present, give up. */
+ if (strncmp(cp, "/dev/", 5) != 0)
+ goto errout;
+ cp += 5;
+
+ /*
+ * For md devices, we treat them all as if they were all
+ * on one disk, since we don't know how to parallelize them.
+ */
+ if (cp[0] == 'm' && cp[1] == 'd') {
+ *(cp+2) = 0;
+ return str;
+ }
+
+ /* Handle DAC 960 devices */
+ if (strncmp(cp, "rd/", 3) == 0) {
+ cp += 3;
+ if (cp[0] != 'c' || cp[2] != 'd' ||
+ !isdigit(cp[1]) || !isdigit(cp[3]))
+ goto errout;
+ *(cp+4) = 0;
+ return str;
+ }
+
+ /* Now let's handle /dev/hd* and /dev/sd* devices.... */
+ if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
+ cp += 2;
+ /* If there's a single number after /dev/hd, skip it */
+ if (isdigit(*cp))
+ cp++;
+ /* What follows must be an alpha char, or give up */
+ if (!isalpha(*cp))
+ goto errout;
+ *(cp + 1) = 0;
+ return str;
+ }
+
+#ifdef CONFIG_FEATURE_DEVFS
+ /* Now let's handle devfs (ugh) names */
+ len = 0;
+ if (strncmp(cp, "ide/", 4) == 0)
+ len = 4;
+ if (strncmp(cp, "scsi/", 5) == 0)
+ len = 5;
+ if (len) {
+ cp += len;
+ /*
+ * Now we proceed down the expected devfs hierarchy.
+ * i.e., .../host1/bus2/target3/lun4/...
+ * If we don't find the expected token, followed by
+ * some number of digits at each level, abort.
+ */
+ for (hier = devfs_hier; *hier; hier++) {
+ len = strlen(*hier);
+ if (strncmp(cp, *hier, len) != 0)
+ goto errout;
+ cp += len;
+ while (*cp != '/' && *cp != 0) {
+ if (!isdigit(*cp))
+ goto errout;
+ cp++;
+ }
+ cp++;
+ }
+ *(cp - 1) = 0;
+ return str;
+ }
+
+ /* Now handle devfs /dev/disc or /dev/disk names */
+ disk = 0;
+ if (strncmp(cp, "discs/", 6) == 0)
+ disk = "disc";
+ else if (strncmp(cp, "disks/", 6) == 0)
+ disk = "disk";
+ if (disk) {
+ cp += 6;
+ if (strncmp(cp, disk, 4) != 0)
+ goto errout;
+ cp += 4;
+ while (*cp != '/' && *cp != 0) {
+ if (!isdigit(*cp))
+ goto errout;
+ cp++;
+ }
+ *cp = 0;
+ return str;
+ }
+#endif
+
+errout:
+ free(str);
+ return NULL;
+}
+
+
static const char * const ignored_types[] = {
"ignore",
"iso9660",
static char *fstype;
static struct fs_info *filesys_info, *filesys_last;
static struct fsck_instance *instance_list;
-static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
static char *fsck_path;
static blkid_cache cache;
struct fs_info *fs;
if ((f = fopen(filename, "r")) == NULL) {
- bb_perror_msg("WARNING: couldn't open %s: %m", filename);
+ bb_perror_msg("WARNING: cannot open %s", filename);
return;
}
while (!feof(f)) {
tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
- s = bb_xasprintf(tpl, s, type);
+ s = xasprintf(tpl, s, type);
if (stat(s, &st) == 0) break;
free(s);
}
return ENOMEM;
memset(inst, 0, sizeof(struct fsck_instance));
- prog = bb_xasprintf("fsck.%s", type);
+ prog = xasprintf("fsck.%s", type);
argv[0] = prog;
argc = 1;
mntpt ? mntpt : device);
for (i=0; i < argc; i++)
printf("%s ", argv[i]);
- printf("\n");
+ puts("");
}
/* Fork and execute the correct program. */
if ((errno == EINTR) || (errno == EAGAIN))
continue;
if (errno == ECHILD) {
- bb_error_msg("wait: No more child process?!?");
+ bb_error_msg("wait: no more child process?!?");
return NULL;
}
perror("wait");
num_running++;
retval = execute(type, fs->device, fs->mountpt, interactive);
if (retval) {
- bb_error_msg("Error %d while executing fsck.%s for %s",
+ bb_error_msg("error %d while executing fsck.%s for %s",
retval, type, fs->device);
num_running--;
}
#define FS_TYPE_OPT 1
#define FS_TYPE_NEGOPT 2
-static const char *fs_type_syntax_error =
-N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
- "with 'no' or '!'.\n");
+static const char fs_type_syntax_error[] =
+"Either all or none of the filesystem types passed to -t must be prefixed\n"
+ "with 'no' or '!'.";
static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
{
}
}
- cmp->list = xcalloc(num, sizeof(char *));
- cmp->type = xcalloc(num, sizeof(int));
+ cmp->list = xzalloc(num * sizeof(char *));
+ cmp->type = xzalloc(num * sizeof(int));
cmp->negate = 0;
if (!fs_type)
bb_error_msg_and_die("%s", fs_type_syntax_error);
}
}
-#if 0
- printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
-#endif
cmp->list[num++] = string_copy(s);
s = strtok(NULL, ",");
}
/* Check if we should ignore this filesystem. */
static int ignore(struct fs_info *fs)
{
- const char * const *ip;
- int wanted = 0;
+ int wanted;
char *s;
/*
if (!fs_match(fs, &fs_type_compiled)) return 1;
/* Are we ignoring this type? */
- for(ip = ignored_types; *ip; ip++)
- if (strcmp(fs->type, *ip) == 0) return 1;
+ if (index_in_str_array(ignored_types, fs->type) >= 0)
+ return 1;
/* Do we really really want to check this fs? */
- for(ip = really_wanted; *ip; ip++)
- if (strcmp(fs->type, *ip) == 0) {
- wanted = 1;
- break;
- }
+ wanted = index_in_str_array(really_wanted, fs->type) >= 0;
/* See if the <fsck.fs> program is available. */
s = find_fsck(fs->type);
return status;
}
-#if 0
-static void usage(void)
-{
- fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
- exit(EXIT_USAGE);
-}
-#endif
-
static void signal_cancel(int sig FSCK_ATTR((unused)))
{
cancel_requested++;
* /proc/partitions isn't found.
*/
if (access("/proc/partitions", R_OK) < 0) {
- bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
- "Is /proc mounted?");
+ bb_perror_msg_and_die("cannot open /proc/partitions "
+ "(is /proc mounted?)");
}
/*
* Check to see if this is because
*/
if (geteuid())
bb_error_msg_and_die(
- "Must be root to scan for matching filesystems: %s\n", arg);
+ "must be root to scan for matching filesystems: %s\n", arg);
else
bb_error_msg_and_die(
- "Couldn't find matching filesystem: %s", arg);
+ "cannot find matching filesystem: %s", arg);
}
devices[num_devices++] = dev ? dev : string_copy(arg);
continue;
case 't':
tmp = 0;
if (fstype)
- usage();
+ bb_show_usage();
if (arg[j+1])
tmp = arg+j+1;
else if ((i+1) < argc)
tmp = argv[++i];
else
- usage();
+ bb_show_usage();
fstype = string_copy(tmp);
compile_fs_type(fstype, &fs_type_compiled);
goto next_arg;
opts_for_fsck++;
break;
case '?':
- usage();
+ bb_show_usage();
break;
default:
options[++opt] = arg[j];
{
int i, status = 0;
int interactive = 0;
- char *oldpath = getenv("PATH");
const char *fstab;
struct fs_info *fs;
fstab = _PATH_MNTTAB;
load_fs_info(fstab);
- /* Update our search path to include uncommon directories. */
- if (oldpath) {
- fsck_path = bb_xasprintf("%s:%s", fsck_prefix_path, oldpath);
- } else {
- fsck_path = string_copy(fsck_prefix_path);
- }
+ fsck_path = e2fs_set_sbin_path();
if ((num_devices == 1) || (serialize))
interactive = 1;
}
}
status |= wait_many(FLAG_WAIT_ALL);
- if (ENABLE_FEATURE_CLEAN_UP)
- free(fsck_path);
blkid_put_cache(cache);
return status;
}