// Recursively delete contents of rootfs.
static void delete_contents(const char *directory, dev_t rootdev)
{
	DIR *dir;
	struct dirent *d;
	struct stat st;

	// Don't descend into other filesystems
	if (lstat(directory, &st) || st.st_dev != rootdev)
		return;

	// Recursively delete the contents of directories.
	if (S_ISDIR(st.st_mode)) {
		dir = opendir(directory);
		if (dir) {
			while ((d = readdir(dir))) {
				char *newdir = d->d_name;

				// Skip . and ..
				if (DOT_OR_DOTDOT(newdir))
					continue;

				// Recurse to delete contents
				newdir = concat_path_file(directory, newdir);
				delete_contents(newdir, rootdev);
				free(newdir);
			}
			closedir(dir);

			// Directory should now be empty.  Zap it.
			rmdir(directory);
		}

	// It wasn't a directory.  Zap it.
	} else unlink(directory);
}
Exemple #2
0
static void delete_contents(const char *directory)
{
	DIR *dir;
	struct dirent *d;
	struct stat st;

	// Don't descend into other filesystems
	if (lstat(directory, &st) || st.st_dev != rootdev) return;

	// Recursively delete the contents of directories.
	if (S_ISDIR(st.st_mode)) {
		dir = opendir(directory);
		if (dir) {
			while ((d = readdir(dir))) {
				char *newdir = d->d_name;

				// Skip . and ..
				if (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
					continue;

				// Recurse to delete contents
				newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
				sprintf(newdir, "%s/%s", directory, d->d_name);
				delete_contents(newdir);
			}
			closedir(dir);

			// Directory should now be empty.  Zap it.
			rmdir(directory);
		}

	// It wasn't a directory.  Zap it.

	} else unlink(directory);
}
Exemple #3
0
int switch_root_main(int argc, char **argv)
{
	char *newroot, *console = NULL;
	struct stat st1, st2;
	struct statfs stfs;

	// Parse args (-c console)

	opt_complementary = "-2";
	getopt32(argv, "c:", &console);
	argv += optind;

	// Change to new root directory and verify it's a different fs.

	newroot = *argv++;

	xchdir(newroot);
	if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
		bb_error_msg_and_die("bad newroot %s", newroot);
	}
	rootdev = st2.st_dev;

	// 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
	// from all the people who WILL eat their filesystems.)

	if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
		(stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
		getpid() != 1)
	{
		bb_error_msg_and_die("not rootfs");
	}

	// Zap everything out of rootdev

	delete_contents("/");

	// Overmount / with newdir and chroot into it.  The chdir is needed to
	// recalculate "." and ".." links.

	if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot("."))
		bb_error_msg_and_die("error moving root");
	xchdir("/");

	// If a new console specified, redirect stdin/stdout/stderr to that.

	if (console) {
		close(0);
		xopen(console, O_RDWR);
		dup2(0, 1);
		dup2(0, 2);
	}

	// Exec real init.  (This is why we must be pid 1.)
	execv(argv[0], argv);
	bb_perror_msg_and_die("bad init %s", argv[0]);
}
Exemple #4
0
int switch_root_main(int argc UNUSED_PARAM, char **argv)
{
	char *newroot, *console = NULL;
	struct stat st;
	struct statfs stfs;
	dev_t rootdev;

	// Parse args (-c console)
	opt_complementary = "-2"; // minimum 2 params
	getopt32(argv, "+c:", &console); // '+': stop at first non-option
	argv += optind;
	newroot = *argv++;

	// Change to new root directory and verify it's a different fs
	xchdir(newroot);
	xstat("/", &st);
	rootdev = st.st_dev;
	xstat(".", &st);
	if (st.st_dev == rootdev || getpid() != 1) {
		// Show usage, it says new root must be a mountpoint
		// and we must be PID 1
		bb_show_usage();
	}

	// 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
	// from all the people who WILL destroy their filesystems.
	if (stat("/init", &st) != 0 || !S_ISREG(st.st_mode)) {
		bb_error_msg_and_die("/init is not a regular file");
	}
	statfs("/", &stfs); // this never fails
	if ((unsigned)stfs.f_type != RAMFS_MAGIC
	 && (unsigned)stfs.f_type != TMPFS_MAGIC
	) {
		bb_error_msg_and_die("root filesystem is not ramfs/tmpfs");
	}

	// Zap everything out of rootdev
	delete_contents("/", rootdev);

	// Overmount / with newdir and chroot into it
	if (mount(".", "/", NULL, MS_MOVE, NULL)) {
		// For example, fails when newroot is not a mountpoint
		bb_perror_msg_and_die("error moving root");
	}
	xchroot(".");
	// The chdir is needed to recalculate "." and ".." links
	/*xchdir("/"); - done in xchroot */

	// If a new console specified, redirect stdin/stdout/stderr to it
	if (console) {
		close(0);
		xopen(console, O_RDWR);
		xdup2(0, 1);
		xdup2(0, 2);
	}

	// Exec real init
	execv(argv[0], argv);
	bb_perror_msg_and_die("can't execute '%s'", argv[0]);
}