[01/11] namespaces: add switch_ns_by_fd

Submitted by Andrei Vagin on Feb. 2, 2017, 12:04 a.m.

Details

Message ID 1485993871-3990-2-git-send-email-avagin@openvz.org
State New
Series "Dump and restore nested network namespaces"
Headers show

Commit Message

Andrei Vagin Feb. 2, 2017, 12:04 a.m.
From: Andrei Vagin <avagin@virtuozzo.com>

It's like switch_ns, but it gets a namespace file descriptor instead of pid.

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 criu/mount.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/mount.c b/criu/mount.c
index 4ba6cc2..190b61e 100644
--- a/criu/mount.c
+++ b/criu/mount.c
@@ -2840,15 +2840,7 @@  int depopulate_roots_yard(int mntns_fd, bool only_ghosts)
 		return -1;
 	}
 
-	old_ns = open_proc(PROC_SELF, "ns/mnt");
-	if (old_ns < 0) {
-		pr_perror("`- Can't keep old ns");
-		close(old_cwd);
-		return -1;
-	}
-	if (setns(mntns_fd, CLONE_NEWNS) < 0) {
-		pr_perror("`- Can't switch");
-		close(old_ns);
+	if (switch_ns_by_fd(mntns_fd, &mnt_ns_desc, &old_ns)) {
 		close(old_cwd);
 		return -1;
 	}
@@ -2859,11 +2851,8 @@  int depopulate_roots_yard(int mntns_fd, bool only_ghosts)
 	if (__depopulate_roots_yard())
 		ret = -1;
 
-	if (setns(old_ns, CLONE_NEWNS) < 0) {
-		pr_perror("Fail to switch back!");
+	if (restore_ns(old_ns, &mnt_ns_desc))
 		ret = -1;
-	}
-	close(old_ns);
 
 	if (fchdir(old_cwd)) {
 		pr_perror("Unable to restore cwd");

Comments

Pavel Emelianov Feb. 8, 2017, 9:31 a.m.
On 02/02/2017 03:04 AM, Andrei Vagin wrote:
> From: Andrei Vagin <avagin@virtuozzo.com>
> 
> It's like switch_ns, but it gets a namespace file descriptor instead of pid.

It looks like switch_ns_by_fd is already added and this patch just
makes use of it in mount.c... How should I change the subject and
the comment of this patch?

> Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
> ---
>  criu/mount.c | 15 ++-------------
>  1 file changed, 2 insertions(+), 13 deletions(-)
> 
> diff --git a/criu/mount.c b/criu/mount.c
> index 4ba6cc2..190b61e 100644
> --- a/criu/mount.c
> +++ b/criu/mount.c
> @@ -2840,15 +2840,7 @@ int depopulate_roots_yard(int mntns_fd, bool only_ghosts)
>  		return -1;
>  	}
>  
> -	old_ns = open_proc(PROC_SELF, "ns/mnt");
> -	if (old_ns < 0) {
> -		pr_perror("`- Can't keep old ns");
> -		close(old_cwd);
> -		return -1;
> -	}
> -	if (setns(mntns_fd, CLONE_NEWNS) < 0) {
> -		pr_perror("`- Can't switch");
> -		close(old_ns);
> +	if (switch_ns_by_fd(mntns_fd, &mnt_ns_desc, &old_ns)) {
>  		close(old_cwd);
>  		return -1;
>  	}
> @@ -2859,11 +2851,8 @@ int depopulate_roots_yard(int mntns_fd, bool only_ghosts)
>  	if (__depopulate_roots_yard())
>  		ret = -1;
>  
> -	if (setns(old_ns, CLONE_NEWNS) < 0) {
> -		pr_perror("Fail to switch back!");
> +	if (restore_ns(old_ns, &mnt_ns_desc))
>  		ret = -1;
> -	}
> -	close(old_ns);
>  
>  	if (fchdir(old_cwd)) {
>  		pr_perror("Unable to restore cwd");
>