summaryrefslogtreecommitdiffstats
path: root/src/shared/switch-root.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-08-03 21:03:29 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2023-08-03 21:05:21 +0200
commit2159662608a00232f94302bd5942d07830c279b4 (patch)
treec687788997fb3e7d853b4fb871bf7c119619760b /src/shared/switch-root.c
parentshutdown: do not umount recursively before MS_MOVE (diff)
downloadsystemd-2159662608a00232f94302bd5942d07830c279b4.tar.xz
systemd-2159662608a00232f94302bd5942d07830c279b4.zip
switch-root: reopen target directory after it is mounted
Fixes a bug introduced by f717d7a40a696b351415976f22a4f498c401de41.
Diffstat (limited to 'src/shared/switch-root.c')
-rw-r--r--src/shared/switch-root.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/shared/switch-root.c b/src/shared/switch-root.c
index 99036c1364..9fb9a3e376 100644
--- a/src/shared/switch-root.c
+++ b/src/shared/switch-root.c
@@ -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);