[CRIU] [PATCH 1/2] ns: Introduce ns type

Pavel Emelyanov xemul at parallels.com
Fri Sep 18 07:03:39 PDT 2015


We (may) have 3 types of namespace objects in criu -- criu's one,
root task's one and others. All of them sometimes make sense and
we differentiate them in a weird way -- by checking the ns->pid
field against getpid() or by comparing with root_item's.

The proposal is to mark ns_id objects explicitly with type field.

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 include/namespaces.h | 10 +++++++++-
 namespaces.c         | 22 ++++++++++++++++++----
 pstree.c             |  2 +-
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/include/namespaces.h b/include/namespaces.h
index 52d2f34..5b2465b 100644
--- a/include/namespaces.h
+++ b/include/namespaces.h
@@ -9,12 +9,20 @@ struct ns_desc {
 	size_t		len;
 };
 
+enum ns_type {
+	NS_UNKNOWN = 0,
+	NS_CRIU,
+	NS_ROOT,
+	NS_OTHER,
+};
+
 struct ns_id {
 	unsigned int kid;
 	unsigned int id;
 	pid_t pid;
 	struct ns_desc *nd;
 	struct ns_id *next;
+	enum ns_type type;
 
 	/*
 	 * For mount namespaces on restore -- indicates that
@@ -68,7 +76,7 @@ extern int restore_ns(int rst, struct ns_desc *nd);
 extern int dump_task_ns_ids(struct pstree_item *);
 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);
-extern int rst_add_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd);
+extern int rst_add_ns_id(unsigned int id, struct pstree_item *, struct ns_desc *nd);
 extern struct ns_id *lookup_ns_by_id(unsigned int id, struct ns_desc *nd);
 
 extern int collect_user_namespaces(bool for_dump);
diff --git a/namespaces.c b/namespaces.c
index 9ce4783..8d7c312 100644
--- a/namespaces.c
+++ b/namespaces.c
@@ -135,12 +135,14 @@ static void nsid_add(struct ns_id *ns, struct ns_desc *nd, unsigned int id, pid_
 	pr_info("Add %s ns %d pid %d\n", nd->str, ns->id, ns->pid);
 }
 
-struct ns_id *rst_new_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd)
+static struct ns_id *__rst_new_ns_id(unsigned int id, pid_t pid,
+		struct ns_desc *nd, enum ns_type type)
 {
 	struct ns_id *nsid;
 
 	nsid = shmalloc(sizeof(*nsid));
 	if (nsid) {
+		nsid->type = type;
 		nsid_add(nsid, nd, id, pid);
 		futex_set(&nsid->ns_created, 0);
 	}
@@ -148,8 +150,14 @@ struct ns_id *rst_new_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd)
 	return nsid;
 }
 
-int rst_add_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd)
+struct ns_id *rst_new_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd)
+{
+	return __rst_new_ns_id(id, pid, nd, NS_CRIU);
+}
+
+int rst_add_ns_id(unsigned int id, struct pstree_item *i, struct ns_desc *nd)
 {
+	pid_t pid = i->pid.virt;
 	struct ns_id *nsid;
 
 	nsid = lookup_ns_by_id(id, nd);
@@ -159,7 +167,8 @@ int rst_add_ns_id(unsigned int id, pid_t pid, struct ns_desc *nd)
 		return 0;
 	}
 
-	nsid = rst_new_ns_id(id, pid, nd);
+	nsid = __rst_new_ns_id(id, pid, nd,
+			i == root_item ? NS_ROOT : NS_OTHER);
 	if (nsid == NULL)
 		return -1;
 
@@ -232,27 +241,32 @@ static unsigned int generate_ns_id(int pid, unsigned int kid, struct ns_desc *nd
 		struct ns_id **ns_ret)
 {
 	struct ns_id *nsid;
+	enum ns_type type;
 
 	nsid = lookup_ns_by_kid(kid, nd);
 	if (nsid)
 		goto found;
 
 	if (pid != getpid()) {
+		type = NS_OTHER;
 		if (pid == root_item->pid.real) {
 			BUG_ON(root_ns_mask & nd->cflag);
 			pr_info("Will take %s namespace in the image\n", nd->str);
 			root_ns_mask |= nd->cflag;
+			type = NS_ROOT;
 		} else if (nd->cflag & ~CLONE_SUBNS) {
 			pr_err("Can't dump nested %s namespace for %d\n",
 					nd->str, pid);
 			return 0;
 		}
-	}
+	} else
+		type = NS_CRIU;
 
 	nsid = xmalloc(sizeof(*nsid));
 	if (!nsid)
 		return 0;
 
+	nsid->type = type;
 	nsid->kid = kid;
 	nsid_add(nsid, nd, ns_next_id++, pid);
 
diff --git a/pstree.c b/pstree.c
index 2dbcb04..18e5a8e 100644
--- a/pstree.c
+++ b/pstree.c
@@ -439,7 +439,7 @@ static int read_pstree_image(void)
 			goto err;
 
 		if (pi->ids->has_mnt_ns_id) {
-			if (rst_add_ns_id(pi->ids->mnt_ns_id, pi->pid.virt, &mnt_ns_desc))
+			if (rst_add_ns_id(pi->ids->mnt_ns_id, pi, &mnt_ns_desc))
 				goto err;
 		}
 	}
-- 
1.9.3



More information about the CRIU mailing list