2014-04-13 22:07:06 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-04-14 11:48:54 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2014-04-13 22:07:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-04-14 11:48:54 +00:00
|
|
|
#include <string.h>
|
2014-04-13 22:07:06 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/vfs.h>
|
2014-04-14 11:48:54 +00:00
|
|
|
#include <unistd.h>
|
2014-04-13 22:07:06 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-14 13:59:39 +00:00
|
|
|
#define RAMFS_MAGIC 0x858458f6 /* some random number */
|
|
|
|
#define TMPFS_MAGIC 0x01021994
|
|
|
|
|
2014-04-13 22:07:06 +00:00
|
|
|
static void
|
|
|
|
delete_content(const char *dir, dev_t curdevice)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
DIR *d;
|
|
|
|
struct stat st;
|
|
|
|
struct dirent *dent;
|
|
|
|
|
|
|
|
/* don't dive into other filesystems */
|
2014-04-14 10:20:09 +00:00
|
|
|
if (lstat(dir, &st) || st.st_dev != curdevice)
|
2014-04-13 22:07:06 +00:00
|
|
|
return;
|
|
|
|
/* delete contents recursively */
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
d = opendir(dir);
|
|
|
|
if (d) {
|
|
|
|
for(; (dent = readdir(d)) ;) {
|
|
|
|
/* skip ".." and "." */
|
2014-04-14 10:32:30 +00:00
|
|
|
if (strcmp(dent->d_name, ".") == 0 ||
|
|
|
|
strcmp(dent->d_name, "..") == 0)
|
2014-04-13 22:07:06 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* build path and dive deeper */
|
|
|
|
strlcat(path, dir, sizeof(path));
|
|
|
|
strlcat(path, dent->d_name, sizeof(path));
|
|
|
|
|
|
|
|
delete_content(path, curdevice);
|
|
|
|
path[0] = 0;
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
/* remove now empty dir */
|
|
|
|
rmdir(dir);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* unlink non-directory */
|
|
|
|
unlink(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-c console] [newroot] [init] (PID 1)\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-04-18 10:49:10 +00:00
|
|
|
main(int argc, char *argv[])
|
2014-04-13 22:07:06 +00:00
|
|
|
{
|
|
|
|
char *console = NULL;
|
|
|
|
dev_t curdev;
|
|
|
|
struct stat st;
|
|
|
|
struct statfs stfs;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
console = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
/* check number of args and if we are PID 1 */
|
2014-04-14 10:20:09 +00:00
|
|
|
if (argc != 2 || getpid() != 1)
|
2014-04-13 22:07:06 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* chdir to newroot and make sure it's a different fs */
|
2014-04-14 10:20:09 +00:00
|
|
|
if (chdir(argv[0]))
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("chdir %s:", argv[0]);
|
2014-04-14 10:20:09 +00:00
|
|
|
|
|
|
|
if (stat("/", &st))
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("stat %s:", "/");
|
2014-04-14 10:20:09 +00:00
|
|
|
|
2014-04-13 22:07:06 +00:00
|
|
|
curdev = st.st_dev;
|
2014-04-14 10:20:09 +00:00
|
|
|
if (stat(".", &st))
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("stat %s:", ".");
|
2014-04-14 10:20:09 +00:00
|
|
|
if (st.st_dev == curdev)
|
2014-04-13 22:07:06 +00:00
|
|
|
usage();
|
|
|
|
|
2014-04-14 10:20:09 +00:00
|
|
|
/* avoids trouble with real filesystems */
|
|
|
|
if (stat("/init", &st) || !S_ISREG(st.st_mode))
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("/init is not a regular file\n");
|
2014-04-14 10:20:09 +00:00
|
|
|
|
2014-04-13 22:07:06 +00:00
|
|
|
statfs("/", &stfs);
|
2014-04-14 10:20:09 +00:00
|
|
|
if ((unsigned)stfs.f_type != RAMFS_MAGIC && (unsigned)stfs.f_type != TMPFS_MAGIC)
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("current filesystem is not a RAMFS or TMPFS\n");
|
|
|
|
|
|
|
|
/* wipe / */
|
|
|
|
delete_content("/", curdev);
|
|
|
|
|
|
|
|
/* overmount / with newroot and chroot into it */
|
2014-04-14 10:20:09 +00:00
|
|
|
if (mount(".", "/", NULL, MS_MOVE, NULL))
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("mount %s:", ".");
|
2014-04-14 10:20:09 +00:00
|
|
|
|
|
|
|
if (chroot("."))
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("chroot failed\n");
|
|
|
|
|
|
|
|
/* if -c is set, redirect stdin/stdout/stderr to console */
|
|
|
|
if (console) {
|
2014-04-14 10:22:28 +00:00
|
|
|
close(STDIN_FILENO);
|
|
|
|
if(open(console, O_RDWR) == -1)
|
2014-04-13 22:07:06 +00:00
|
|
|
eprintf("open %s:", console);
|
2014-04-14 10:22:28 +00:00
|
|
|
if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
|
2014-04-14 10:27:18 +00:00
|
|
|
eprintf("dup2 %s:", "stdin,stdout");
|
2014-04-14 10:22:28 +00:00
|
|
|
if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
|
2014-04-14 10:27:18 +00:00
|
|
|
eprintf("dup2 %s:", "stdin,stderr");
|
2014-04-13 22:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* execute init */
|
|
|
|
execv(argv[1], argv);
|
|
|
|
eprintf("can't execute '%s'\n", argv[1]);
|
2014-04-14 08:46:39 +00:00
|
|
|
return EXIT_FAILURE;
|
2014-04-13 22:07:06 +00:00
|
|
|
}
|