[v2,02/57] ns: Pack functionality of storing ns fd to store_self_ns()

Submitted by Kirill Tkhai on March 28, 2017, 3:34 p.m.

Details

Message ID 149071525342.12770.16042137653665452293.stgit@localhost.localdomain
State New
Series "Nested pid namespaces support"
Headers show

Commit Message

Kirill Tkhai March 28, 2017, 3:34 p.m.
We have the same code in several places. Pack it into separate function.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/include/namespaces.h |    1 +
 criu/namespaces.c         |   24 ++++++++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/include/namespaces.h b/criu/include/namespaces.h
index f65f791e..d92b721b 100644
--- a/criu/include/namespaces.h
+++ b/criu/include/namespaces.h
@@ -193,6 +193,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 3a24099c..021cb020 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -2163,6 +2163,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,
@@ -2182,8 +2196,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)
@@ -2194,13 +2208,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;