]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
switch-root: reopen target directory after it is mounted 28648/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 3 Aug 2023 19:03:29 +0000 (04:03 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 3 Aug 2023 19:05:21 +0000 (04:05 +0900)
Fixes a bug introduced by f717d7a40a696b351415976f22a4f498c401de41.

src/shared/switch-root.c

index 99036c1364736195ce4d6edb8fff63331e61c22d..9fb9a3e3768ef426745e2ab733c8ea8444b42a08 100644 (file)
@@ -76,6 +76,18 @@ int switch_root(const char *new_root,
         r = fd_make_mount_point(new_root_fd);
         if (r < 0)
                 return log_error_errno(r, "Failed to make new root directory a mount point: %m");
+        if (r > 0) {
+                int fd;
+
+                /* When the path was not a mount point, then we need to reopen the path, otherwise, it still
+                 * points to the underlying directory. */
+
+                fd = open(new_root, O_DIRECTORY|O_CLOEXEC);
+                if (fd < 0)
+                        return log_error_errno(errno, "Failed to reopen target directory '%s': %m", new_root);
+
+                close_and_replace(new_root_fd, fd);
+        }
 
         if (FLAGS_SET(flags, SWITCH_ROOT_DESTROY_OLD_ROOT)) {
                 istmp = fd_is_temporary_fs(old_root_fd);