diff options
-rw-r--r-- | util-linux/switch_root.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c index 242699560..0c86eec08 100644 --- a/util-linux/switch_root.c +++ b/util-linux/switch_root.c | |||
@@ -56,17 +56,17 @@ static void delete_contents(char *directory) | |||
56 | closedir(dir); | 56 | closedir(dir); |
57 | 57 | ||
58 | // Directory should now be empty. Zap it. | 58 | // Directory should now be empty. Zap it. |
59 | printf("rmdir %s\n",directory); // rmdir(directory); | 59 | rmdir(directory); |
60 | } | 60 | } |
61 | 61 | ||
62 | // It wasn't a directory. Zap it. | 62 | // It wasn't a directory. Zap it. |
63 | 63 | ||
64 | } else printf("unlink %s\n",directory); //unlink(directory); | 64 | } else unlink(directory); |
65 | } | 65 | } |
66 | 66 | ||
67 | int switch_root_main(int argc, char *argv[]) | 67 | int switch_root_main(int argc, char *argv[]) |
68 | { | 68 | { |
69 | char *newroot, *console="/dev/console"; | 69 | char *newroot, *console=NULL; |
70 | struct stat st1, st2; | 70 | struct stat st1, st2; |
71 | struct statfs stfs; | 71 | struct statfs stfs; |
72 | 72 | ||
@@ -98,19 +98,24 @@ int switch_root_main(int argc, char *argv[]) | |||
98 | } | 98 | } |
99 | 99 | ||
100 | // Zap everything out of rootdev | 100 | // Zap everything out of rootdev |
101 | |||
101 | delete_contents("/"); | 102 | delete_contents("/"); |
102 | 103 | ||
103 | // Overmount / with newdir | 104 | // Overmount / with newdir and chroot into it. The chdir is needed to |
105 | // recalculate "." and ".." links. | ||
104 | 106 | ||
105 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chdir("/")) | 107 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/")) |
106 | bb_error_msg_and_die("moving root"); | 108 | bb_error_msg_and_die("moving root"); |
107 | 109 | ||
108 | // Reopen stdin/stdout/stderr to /dev/console | 110 | // If a new console specified, redirect stdin/stdout/stderr to that. |
109 | close(0); | 111 | |
110 | if(open(console, O_RDWR) < 0) | 112 | if (console) { |
111 | bb_error_msg_and_die("Bad console '%s'",console); | 113 | close(0); |
112 | dup2(0, 1); | 114 | if(open(console, O_RDWR) < 0) |
113 | dup2(0, 2); | 115 | bb_error_msg_and_die("Bad console '%s'",console); |
116 | dup2(0, 1); | ||
117 | dup2(0, 2); | ||
118 | } | ||
114 | 119 | ||
115 | // Exec real init. (This is why we must be pid 1.) | 120 | // Exec real init. (This is why we must be pid 1.) |
116 | execv(argv[optind],argv+optind+1); | 121 | execv(argv[optind],argv+optind+1); |