summaryrefslogtreecommitdiff
path: root/util-linux/switch_root.c
diff options
context:
space:
mode:
authorRob Landley2005-12-20 17:25:51 +0000
committerRob Landley2005-12-20 17:25:51 +0000
commit5d84c2398efe3df87141b647228f8817aad12ab6 (patch)
tree03bda36e6ef91ece33c5bfc2d3c7a64a599f6b94 /util-linux/switch_root.c
parent7b363fd3c904c2530844150d00b99f7b32d97f05 (diff)
downloadbusybox-5d84c2398efe3df87141b647228f8817aad12ab6.zip
busybox-5d84c2398efe3df87141b647228f8817aad12ab6.tar.gz
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...)
Diffstat (limited to 'util-linux/switch_root.c')
-rw-r--r--util-linux/switch_root.c27
1 files changed, 16 insertions, 11 deletions
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c
index 2426995..0c86eec 100644
--- a/util-linux/switch_root.c
+++ b/util-linux/switch_root.c
@@ -56,17 +56,17 @@ static void delete_contents(char *directory)
closedir(dir);
// Directory should now be empty. Zap it.
- printf("rmdir %s\n",directory); // rmdir(directory);
+ rmdir(directory);
}
// 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[])
{
- char *newroot, *console="/dev/console";
+ char *newroot, *console=NULL;
struct stat st1, st2;
struct statfs stfs;
@@ -98,19 +98,24 @@ int switch_root_main(int argc, char *argv[])
}
// Zap everything out of rootdev
+
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");
- // Reopen stdin/stdout/stderr to /dev/console
- close(0);
- if(open(console, O_RDWR) < 0)
- bb_error_msg_and_die("Bad console '%s'",console);
- dup2(0, 1);
- dup2(0, 2);
+ // If a new console specified, redirect stdin/stdout/stderr to that.
+
+ if (console) {
+ close(0);
+ if(open(console, O_RDWR) < 0)
+ 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.)
execv(argv[optind],argv+optind+1);