Fix chroot, leave console alone if -c not specified, and yank debug code.

(I still haven't set up a test environment to confirm this works...)
This commit is contained in:
Rob Landley 2005-12-20 17:25:51 +00:00
parent 7b363fd3c9
commit 5d84c2398e

View File

@ -56,17 +56,17 @@ static void delete_contents(char *directory)
closedir(dir); closedir(dir);
// Directory should now be empty. Zap it. // Directory should now be empty. Zap it.
printf("rmdir %s\n",directory); // rmdir(directory); rmdir(directory);
} }
// It wasn't a directory. Zap it. // It wasn't a directory. Zap it.
} else printf("unlink %s\n",directory); //unlink(directory); } else unlink(directory);
} }
int switch_root_main(int argc, char *argv[]) int switch_root_main(int argc, char *argv[])
{ {
char *newroot, *console="/dev/console"; char *newroot, *console=NULL;
struct stat st1, st2; struct stat st1, st2;
struct statfs stfs; struct statfs stfs;
@ -98,19 +98,24 @@ int switch_root_main(int argc, char *argv[])
} }
// Zap everything out of rootdev // Zap everything out of rootdev
delete_contents("/"); delete_contents("/");
// Overmount / with newdir // Overmount / with newdir and chroot into it. The chdir is needed to
// recalculate "." and ".." links.
if (mount(".", "/", NULL, MS_MOVE, NULL) || chdir("/")) if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/"))
bb_error_msg_and_die("moving root"); bb_error_msg_and_die("moving root");
// Reopen stdin/stdout/stderr to /dev/console // If a new console specified, redirect stdin/stdout/stderr to that.
close(0);
if(open(console, O_RDWR) < 0) if (console) {
bb_error_msg_and_die("Bad console '%s'",console); close(0);
dup2(0, 1); if(open(console, O_RDWR) < 0)
dup2(0, 2); bb_error_msg_and_die("Bad console '%s'",console);
dup2(0, 1);
dup2(0, 2);
}
// Exec real init. (This is why we must be pid 1.) // Exec real init. (This is why we must be pid 1.)
execv(argv[optind],argv+optind+1); execv(argv[optind],argv+optind+1);