core,nspawn: unify code that moves the root dir
authorLennart Poettering <lennart@poettering.net>
Tue, 19 May 2015 18:32:44 +0000 (20:32 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 May 2015 12:38:12 +0000 (14:38 +0200)
src/core/namespace.c
src/nspawn/nspawn.c
src/shared/util.c
src/shared/util.h

index 5b78279..c0c64fd 100644 (file)
@@ -125,22 +125,6 @@ static void drop_duplicates(BindMount *m, unsigned *n) {
         *n = t - m;
 }
 
-static int mount_move_root(const char *path) {
-        if (chdir(path) < 0)
-                return -errno;
-
-        if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
-                return -errno;
-
-        if (chroot(".") < 0)
-                return -errno;
-
-        if (chdir("/") < 0)
-                return -errno;
-
-        return 0;
-}
-
 static int mount_dev(BindMount *m) {
         static const char devnodes[] =
                 "/dev/null\0"
index a38f47d..1f919c0 100644 (file)
@@ -4391,23 +4391,9 @@ int main(int argc, char *argv[]) {
                         if (mount_cgroup(arg_directory) < 0)
                                 _exit(EXIT_FAILURE);
 
-                        if (chdir(arg_directory) < 0) {
-                                log_error_errno(errno, "chdir(%s) failed: %m", arg_directory);
-                                _exit(EXIT_FAILURE);
-                        }
-
-                        if (mount(arg_directory, "/", NULL, MS_MOVE, NULL) < 0) {
-                                log_error_errno(errno, "mount(MS_MOVE) failed: %m");
-                                _exit(EXIT_FAILURE);
-                        }
-
-                        if (chroot(".") < 0) {
-                                log_error_errno(errno, "chroot() failed: %m");
-                                _exit(EXIT_FAILURE);
-                        }
-
-                        if (chdir("/") < 0) {
-                                log_error_errno(errno, "chdir() failed: %m");
+                        r = mount_move_root(arg_directory);
+                        if (r < 0) {
+                                log_error_errno(r, "Failed to move root directory: %m");
                                 _exit(EXIT_FAILURE);
                         }
 
index e18645f..c3b08bb 100644 (file)
@@ -6229,3 +6229,21 @@ int parse_mode(const char *s, mode_t *ret) {
         *ret = (mode_t) l;
         return 0;
 }
+
+int mount_move_root(const char *path) {
+        assert(path);
+
+        if (chdir(path) < 0)
+                return -errno;
+
+        if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
+                return -errno;
+
+        if (chroot(".") < 0)
+                return -errno;
+
+        if (chdir("/") < 0)
+                return -errno;
+
+        return 0;
+}
index 0e806cf..f0382f0 100644 (file)
@@ -906,3 +906,5 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
 char *shell_maybe_quote(const char *s);
 
 int parse_mode(const char *s, mode_t *ret);
+
+int mount_move_root(const char *path);