[CRIU] [PATCH] mount: restore cwd after creating a roots yard
Andrey Vagin
avagin at openvz.org
Thu Dec 3 11:58:37 PST 2015
From: Andrew Vagin <avagin at virtuozzo.com>
Currently we see that a cgroup yard are not umounted
with the ENOENT error, because cwd was changed.
Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
mount.c | 36 +++++++++++++++++++++++++++++++-----
1 file changed, 31 insertions(+), 5 deletions(-)
diff --git a/mount.c b/mount.c
index ca10d43..6a9651c 100644
--- a/mount.c
+++ b/mount.c
@@ -2325,27 +2325,43 @@ static inline int print_ns_root(struct ns_id *ns, char *buf, int bs)
static int create_mnt_roots(void)
{
+ int exit_code = -1, cwd_fd;
+
if (mnt_roots)
return 0;
+ cwd_fd = open(".", O_DIRECTORY);
+ if (cwd_fd < 0) {
+ pr_perror("Unable to open cwd");
+ return -1;
+ }
+
if (chdir(opts.root ? : "/")) {
pr_perror("Unable to change working directory on %s", opts.root);
- return -1;
+ goto out;
}
mnt_roots = strdup(".criu.mntns.XXXXXX");
if (mnt_roots == NULL) {
pr_perror("Can't allocate memory");
- return -1;
+ goto out;
}
if (mkdtemp(mnt_roots) == NULL) {
pr_perror("Unable to create a temporary directory");
mnt_roots = NULL;
- return -1;
+ goto out;
}
- return 0;
+ exit_code = 0;
+out:
+ if (fchdir(cwd_fd)) {
+ pr_perror("Unable to restore cwd");
+ exit_code = -1;
+ }
+ close(cwd_fd);
+
+ return exit_code;
}
static int rst_collect_local_mntns(void)
@@ -2756,11 +2772,21 @@ int depopulate_roots_yard(void)
void cleanup_mnt_ns(void)
{
+ char path[PATH_MAX], *root = opts.root ? : "/";
+ int fd;
+
if (mnt_roots == NULL)
return;
- if (rmdir(mnt_roots))
+ fd = open(root, O_DIRECTORY | O_RDONLY);
+ if (fd < 0) {
+ pr_perror("Unable to open %s", opts.root);
+ return;
+ }
+ snprintf(path, sizeof(path), "/proc/self/fd/%d/%s", fd, mnt_roots);
+ if (rmdir(path))
pr_perror("Can't remove the directory %s", mnt_roots);
+ close(fd);
}
int prepare_mnt_ns(void)
--
2.4.3
More information about the CRIU
mailing list