switch_root: shrink
function old new delta switch_root_main 402 401 -1 rootdev 8 - -8 delete_contents 226 179 -47 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 0/2 up/down: 0/-56) Total: -56 bytes
This commit is contained in:
parent
e7067e38ea
commit
39acf45335
@ -9,9 +9,7 @@
|
|||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
#include <sys/vfs.h>
|
#include <sys/vfs.h>
|
||||||
|
|
||||||
|
|
||||||
// Make up for header deficiencies.
|
// Make up for header deficiencies.
|
||||||
|
|
||||||
#ifndef RAMFS_MAGIC
|
#ifndef RAMFS_MAGIC
|
||||||
#define RAMFS_MAGIC ((unsigned)0x858458f6)
|
#define RAMFS_MAGIC ((unsigned)0x858458f6)
|
||||||
#endif
|
#endif
|
||||||
@ -24,18 +22,16 @@
|
|||||||
#define MS_MOVE 8192
|
#define MS_MOVE 8192
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static dev_t rootdev;
|
|
||||||
|
|
||||||
// Recursively delete contents of rootfs.
|
// Recursively delete contents of rootfs.
|
||||||
|
static void delete_contents(const char *directory, dev_t rootdev)
|
||||||
static void delete_contents(const char *directory)
|
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *d;
|
struct dirent *d;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
// Don't descend into other filesystems
|
// Don't descend into other filesystems
|
||||||
if (lstat(directory, &st) || st.st_dev != rootdev) return;
|
if (lstat(directory, &st) || st.st_dev != rootdev)
|
||||||
|
return;
|
||||||
|
|
||||||
// Recursively delete the contents of directories.
|
// Recursively delete the contents of directories.
|
||||||
if (S_ISDIR(st.st_mode)) {
|
if (S_ISDIR(st.st_mode)) {
|
||||||
@ -45,13 +41,13 @@ static void delete_contents(const char *directory)
|
|||||||
char *newdir = d->d_name;
|
char *newdir = d->d_name;
|
||||||
|
|
||||||
// Skip . and ..
|
// Skip . and ..
|
||||||
if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
|
if (DOT_OR_DOTDOT(newdir))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Recurse to delete contents
|
// Recurse to delete contents
|
||||||
newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
|
newdir = concat_path_file(directory, newdir);
|
||||||
sprintf(newdir, "%s/%s", directory, d->d_name);
|
delete_contents(newdir, rootdev);
|
||||||
delete_contents(newdir);
|
free(newdir);
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
@ -60,7 +56,6 @@ static void delete_contents(const char *directory)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// It wasn't a directory. Zap it.
|
// It wasn't a directory. Zap it.
|
||||||
|
|
||||||
} else unlink(directory);
|
} else unlink(directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,15 +65,14 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
char *newroot, *console = NULL;
|
char *newroot, *console = NULL;
|
||||||
struct stat st1, st2;
|
struct stat st1, st2;
|
||||||
struct statfs stfs;
|
struct statfs stfs;
|
||||||
|
dev_t rootdev;
|
||||||
|
|
||||||
// Parse args (-c console)
|
// Parse args (-c console)
|
||||||
|
|
||||||
opt_complementary = "-2"; // minimum 2 params
|
opt_complementary = "-2"; // minimum 2 params
|
||||||
getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option
|
getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
// Change to new root directory and verify it's a different fs.
|
// Change to new root directory and verify it's a different fs.
|
||||||
|
|
||||||
newroot = *argv++;
|
newroot = *argv++;
|
||||||
|
|
||||||
xchdir(newroot);
|
xchdir(newroot);
|
||||||
@ -90,7 +84,6 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
// Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
|
// Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
|
||||||
// we mean it. (I could make this a CONFIG option, but I would get email
|
// we mean it. (I could make this a CONFIG option, but I would get email
|
||||||
// from all the people who WILL eat their filesystems.)
|
// from all the people who WILL eat their filesystems.)
|
||||||
|
|
||||||
if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs)
|
if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs)
|
||||||
|| (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC))
|
|| (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC))
|
||||||
|| (getpid() != 1)
|
|| (getpid() != 1)
|
||||||
@ -99,24 +92,21 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Zap everything out of rootdev
|
// Zap everything out of rootdev
|
||||||
|
delete_contents("/", rootdev);
|
||||||
delete_contents("/");
|
|
||||||
|
|
||||||
// Overmount / with newdir and chroot into it. The chdir is needed to
|
// Overmount / with newdir and chroot into it. The chdir is needed to
|
||||||
// recalculate "." and ".." links.
|
// recalculate "." and ".." links.
|
||||||
|
|
||||||
if (mount(".", "/", NULL, MS_MOVE, NULL))
|
if (mount(".", "/", NULL, MS_MOVE, NULL))
|
||||||
bb_error_msg_and_die("error moving root");
|
bb_error_msg_and_die("error moving root");
|
||||||
xchroot(".");
|
xchroot(".");
|
||||||
xchdir("/");
|
xchdir("/");
|
||||||
|
|
||||||
// If a new console specified, redirect stdin/stdout/stderr to that.
|
// If a new console specified, redirect stdin/stdout/stderr to that.
|
||||||
|
|
||||||
if (console) {
|
if (console) {
|
||||||
close(0);
|
close(0);
|
||||||
xopen(console, O_RDWR);
|
xopen(console, O_RDWR);
|
||||||
dup2(0, 1);
|
xdup2(0, 1);
|
||||||
dup2(0, 2);
|
xdup2(0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec real init. (This is why we must be pid 1.)
|
// Exec real init. (This is why we must be pid 1.)
|
||||||
|
Loading…
Reference in New Issue
Block a user