aboutsummaryrefslogtreecommitdiff
path: root/util-linux/switch_root.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/switch_root.c')
-rw-r--r--util-linux/switch_root.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c
index 6dba9f05f..88bb9db2a 100644
--- a/util-linux/switch_root.c
+++ b/util-linux/switch_root.c
@@ -75,14 +75,14 @@ int switch_root_main(int argc, char **argv)
75 75
76 opt_complementary = "-2"; 76 opt_complementary = "-2";
77 getopt32(argc, argv, "c:", &console); 77 getopt32(argc, argv, "c:", &console);
78 argv += optind;
78 79
79 // Change to new root directory and verify it's a different fs. 80 // Change to new root directory and verify it's a different fs.
80 81
81 newroot = argv[optind++]; 82 newroot = *argv++;
82 83
83 if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) || 84 xchdir(newroot);
84 st1.st_dev == st2.st_dev) 85 if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
85 {
86 bb_error_msg_and_die("bad newroot %s", newroot); 86 bb_error_msg_and_die("bad newroot %s", newroot);
87 } 87 }
88 rootdev = st2.st_dev; 88 rootdev = st2.st_dev;
@@ -105,8 +105,9 @@ int switch_root_main(int argc, char **argv)
105 // Overmount / with newdir and chroot into it. The chdir is needed to 105 // Overmount / with newdir and chroot into it. The chdir is needed to
106 // recalculate "." and ".." links. 106 // recalculate "." and ".." links.
107 107
108 if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/")) 108 if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot("."))
109 bb_error_msg_and_die("error moving root"); 109 bb_error_msg_and_die("error moving root");
110 xchdir("/");
110 111
111 // If a new console specified, redirect stdin/stdout/stderr to that. 112 // If a new console specified, redirect stdin/stdout/stderr to that.
112 113
@@ -118,6 +119,6 @@ int switch_root_main(int argc, char **argv)
118 } 119 }
119 120
120 // Exec real init. (This is why we must be pid 1.) 121 // Exec real init. (This is why we must be pid 1.)
121 execv(argv[optind], argv+optind); 122 execv(argv[0], argv);
122 bb_error_msg_and_die("bad init '%s'", argv[optind]); 123 bb_perror_msg_and_die("bad init %s", argv[0]);
123} 124}