net: set the root net ns after restoring all namespaces

Submitted by Andrei Vagin on March 28, 2017, 7:56 p.m.

Details

Message ID 1490731015-31886-1-git-send-email-avagin@openvz.org
State New
Series "net: set the root net ns after restoring all namespaces"
Headers show

Commit Message

Andrei Vagin March 28, 2017, 7:56 p.m.
From: Andrei Vagin <avagin@virtuozzo.com>

Then we will create transport sockets and we want to know where
they will be created.

Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 criu/net.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/net.c b/criu/net.c
index e06d32b..0f1736b 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -2035,7 +2035,10 @@  static int prepare_net_ns_second_stage(struct ns_id *ns)
 		ns->net.nsfd_id = fdstore_add(fd);
 		if (ns->net.nsfd_id < 0)
 			ret = -1;
-		close(fd);
+
+		/* fd for the root is closed in prepare_net_namespaces() */
+		if (ns->type != NS_ROOT)
+			close(fd);
 	}
 
 	ns->ns_populated = true;
@@ -2094,7 +2097,7 @@  static int create_net_ns(void *arg)
 int prepare_net_namespaces()
 {
 	struct ns_id *nsid;
-	int ret = -1;
+	int ret = -1, rst = -1;
 
 	if (!(root_ns_mask & CLONE_NEWNET))
 		return 0;
@@ -2137,6 +2140,9 @@  int prepare_net_namespaces()
 		if (nsid->nd != &net_ns_desc)
 			continue;
 
+		if (nsid->type == NS_ROOT)
+			rst = nsid->net.ns_fd;
+
 		if (switch_ns_by_fd(nsid->net.ns_fd, &net_ns_desc, NULL))
 			goto err;
 
@@ -2146,11 +2152,20 @@  int prepare_net_namespaces()
 		close_safe(&nsid->net.nlsk);
 	}
 
+	if (rst < 0) {
+		pr_err("Unable to find the root net namespace");
+		goto err;
+	}
+
+	if (switch_ns_by_fd(rst, &net_ns_desc, NULL))
+		goto err;
+
 	close_service_fd(NS_FD_OFF);
 	ret = 0;
 err:
 	if (ret)
 		pr_err("Can't create net_ns\n");
+	close_safe(&rst);
 
 	return ret;
 }