From 6ac7fe2986fa1b8c323a7602b828cbb658cd6a33 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sun, 2 Sep 2018 01:05:34 +0100 Subject: [PATCH] utils: teach fsck mount and remount about the root name This allows us to stop all the substroot stuff as with fsck split we now have enough space. This actually turns the whole thing into a win. We trade the extra exec's of substroot for a fork/exec of fsck. Not only that but with a bit of thought we can later optimise the single case fsck to kill the fork off --- Applications/util/fsck.c | 4 +++- Applications/util/mount.c | 16 ++++++++++------ Applications/util/remount.c | 35 +++++------------------------------ 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/Applications/util/fsck.c b/Applications/util/fsck.c index d44d869c..b5649929 100644 --- a/Applications/util/fsck.c +++ b/Applications/util/fsck.c @@ -22,13 +22,15 @@ const char *mntpoint(const char *mount) { FILE *fp; struct mntent *mnt; + const char *p; fp = setmntent("/etc/fstab", "r"); if (fp) { while (mnt = getmntent(fp)) { + p = mnt_device_path(mnt); if (strcmp(mnt->mnt_dir, mount) == 0) { endmntent(fp); - return mnt->mnt_fsname; + return p; } } endmntent(fp); diff --git a/Applications/util/mount.c b/Applications/util/mount.c index b07f8c3d..9101e00f 100644 --- a/Applications/util/mount.c +++ b/Applications/util/mount.c @@ -60,14 +60,14 @@ static int flagsof(struct mntent *mnt) return f; } -static int is_mounted(struct mntent *ent) +static int is_mounted(const char *path) { FILE *f = setmntent("/etc/mtab", "r"); struct mntent mnt; static char buf[_MAX_MNTLEN]; while(getmntent_r(f, &mnt, buf, _MAX_MNTLEN)) { - if (strcmp(mnt.mnt_fsname, ent->mnt_fsname) == 0) { + if (strcmp(mnt.mnt_fsname, path) == 0) { endmntent(f); return 1; } @@ -78,11 +78,13 @@ static int is_mounted(struct mntent *ent) static void do_mount(struct mntent *mnt) { + const char *p; if (strcmp(mnt->mnt_type, "swap") == 0) return; - if (mount(mnt->mnt_fsname, mnt->mnt_dir, flagsof(mnt)) == -1) { + p = mnt_device_path(mnt); + if (mount(p, mnt->mnt_dir, flagsof(mnt)) == -1) { err = errno; - perror(mnt->mnt_fsname); + perror(p); return; } add2mtab(mnt); @@ -91,14 +93,16 @@ static void do_mount(struct mntent *mnt) static void automount(char *match) { FILE *f = setmntent("/etc/fstab", "r"); + const char *p; struct mntent *mnt; while (mnt = getmntent(f)) { + p = mnt_device_path(mnt); /* Warning - mnt contents go invalid if we do work on mtab so be careful here and use getmntent_r in is_mounted */ - if (is_mounted(mnt)) + if (is_mounted(p)) continue; - if (match == NULL || strcmp(mnt->mnt_fsname, match) == 0 || + if (match == NULL || strcmp(p, match) == 0 || strcmp(mnt->mnt_dir, match) == 0) do_mount(mnt); if (err) diff --git a/Applications/util/remount.c b/Applications/util/remount.c index 432d56c3..8ed911d7 100644 --- a/Applications/util/remount.c +++ b/Applications/util/remount.c @@ -35,44 +35,19 @@ static char *modify_opts(char *opts, int flags) return optbuf; } -#define DEV_PATH "/dev/" - -static char *root_device(void) -{ - static DIR dp; - struct dirent *entry; - struct stat filestat, rootstat; - static char namebuf[sizeof(DEV_PATH) + MAXNAMLEN + 1]; - - if (stat("/", &rootstat) == 0 - && opendir_r(&dp,DEV_PATH) != (DIR *) NULL) { - while ((entry = readdir(&dp)) != (struct dirent *) NULL) { - strcpy(namebuf, DEV_PATH); - strlcat(namebuf, entry->d_name, sizeof(namebuf)); - if (stat(namebuf, &filestat) != 0) - continue; - if (!S_ISBLK(filestat.st_mode)) - continue; - if (filestat.st_rdev != rootstat.st_dev) - continue; - return namebuf; - } - } - return NULL; -} - - static char *getdev(char *arg, char *p) { FILE *f; struct mntent *mnt; + const char *path; f = setmntent(p, "r"); if (f) { while (mnt = getmntent(f)) { - if ((strcmp(mnt->mnt_fsname, arg) == 0) || (strcmp(mnt->mnt_dir, arg) == 0)) { + path = mnt_device_path(mnt); + if ((strcmp(path, arg) == 0) || (strcmp(mnt->mnt_dir, arg) == 0)) { endmntent(f); - return strdup(mnt->mnt_fsname); + return strdup(path); } } endmntent(f); @@ -81,7 +56,7 @@ static char *getdev(char *arg, char *p) we want it to do the right thing anyway so you can clean up nicely */ if (strcmp(arg, "/")) return NULL; - return root_device(); + return root_device_name(); } static void rewrite_mtab(char *name, int flags) -- 2.34.1