[CRIU] [PATCH RESEND v1 02/55] ns: Pack functionality of storing ns fd to store_self_ns()

Kirill Tkhai ktkhai at virtuozzo.com
Fri Mar 24 08:09:23 PDT 2017


We have the same code in several places. Pack it into separate function.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/include/namespaces.h |    1 +
 criu/namespaces.c         |   24 ++++++++++++++++--------
 criu/net.c                |   23 ++---------------------
 3 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/criu/include/namespaces.h b/criu/include/namespaces.h
index f3278787..fb39ce9c 100644
--- a/criu/include/namespaces.h
+++ b/criu/include/namespaces.h
@@ -163,6 +163,7 @@ extern int predump_task_ns_ids(struct pstree_item *);
 extern struct ns_id *rst_new_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd, enum ns_type t);
 extern int rst_add_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd);
 extern struct ns_id *lookup_ns_by_id(unsigned int id, struct ns_desc *nd);
+extern int store_self_ns(struct ns_id *ns);
 
 extern int collect_user_namespaces(bool for_dump);
 extern int prepare_userns(pid_t real_pid, UsernsEntry *e);
diff --git a/criu/namespaces.c b/criu/namespaces.c
index d75c1dc1..b6780633 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -2152,6 +2152,20 @@ int join_namespaces(void)
 	return ret;
 }
 
+int store_self_ns(struct ns_id *ns)
+{
+	int fd, id;
+
+	/* Pin one with a file descriptor */
+	fd = open_proc(PROC_SELF, "ns/%s", ns->nd->str);
+	if (fd < 0)
+		return -1;
+
+	id = fdstore_add(fd);
+	close(fd);
+	return id;
+}
+
 enum {
 	NS__CREATED = 1,
 	NS__MAPS_POPULATED,
@@ -2171,8 +2185,8 @@ static int create_user_ns_hierarhy_fn(void *in_arg)
 	char stack[128] __stack_aligned__;
 	struct ns_arg *arg = NULL, *p_arg = in_arg;
 	futex_t *p_futex = NULL, *futex = NULL;
-	int status, fd, ret = -1;
 	struct ns_id *me, *child;
+	int status, ret = -1;
 	pid_t pid = -1;
 
 	if (p_arg->me != root_user_ns)
@@ -2183,13 +2197,7 @@ static int create_user_ns_hierarhy_fn(void *in_arg)
 		/* Set self pid to allow parent restore user_ns maps */
 		p_arg->pid = get_self_real_pid();
 		futex_set_and_wake(p_futex, NS__CREATED);
-		fd = open("/proc/self/ns/user", O_RDONLY);
-		if (fd < 0) {
-			pr_perror("Can't get self user ns");
-			goto out;
-		}
-		me->user.nsfd_id = fdstore_add(fd);
-		close(fd);
+		me->user.nsfd_id = store_self_ns(me);
 		if (me->user.nsfd_id < 0) {
 			pr_err("Can't add fd to fdstore\n");
 			goto out;
diff --git a/criu/net.c b/criu/net.c
index ab74e918..a5d3df6b 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -1701,26 +1701,6 @@ static int prepare_net_ns(int nsid)
 	return ret;
 }
 
-static int open_net_ns(struct ns_id *nsid)
-{
-	int fd, id;
-
-	/* Pin one with a file descriptor */
-	fd = open_proc(PROC_SELF, "ns/net");
-	if (fd < 0)
-		return -1;
-
-	id = fdstore_add(fd);
-	close(fd);
-	if (id < 0) {
-		return -1;
-	}
-
-	nsid->net.nsfd_id = id;
-
-	return 0;
-}
-
 static int do_create_net_ns(struct ns_id *ns)
 {
 	if (unshare(CLONE_NEWNET)) {
@@ -1729,7 +1709,8 @@ static int do_create_net_ns(struct ns_id *ns)
 	}
 	if (prepare_net_ns(ns->id))
 		return -1;
-	if (open_net_ns(ns))
+	ns->net.nsfd_id = store_self_ns(ns);
+	if (ns->net.nsfd_id < 0)
 		return -1;
 	return 0;
 }



More information about the CRIU mailing list