[CRIU] [PATCH v2 18/36] ns: Implement dup_userns_entry()

Kirill Tkhai ktkhai at virtuozzo.com
Fri Feb 3 08:14:16 PST 2017


Function for cloning UsernsEntry entries.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 criu/namespaces.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/criu/namespaces.c b/criu/namespaces.c
index e93c74065..1115736f5 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -493,6 +493,55 @@ int dump_one_ns_file(int lfd, u32 id, const struct fd_parms *p)
 	return pb_write_one(img, &nfe, PB_NS_FILE);
 }
 
+static UsernsEntry *dup_userns_entry(UsernsEntry *orig)
+{
+	UsernsEntry *copy;
+	int ret = -1;
+	size_t *i;
+
+	copy = xzalloc(sizeof(*copy));
+	if (!copy)
+		goto out;
+#define COPY_MAP(map, n_map)								\
+	do {										\
+		i = &copy->n_map;							\
+		copy->map = xmalloc(sizeof(UidGidExtent *) * orig->n_map);		\
+		if (!copy->map)								\
+			goto out;							\
+		for (*i = 0; *i < orig->n_map; ++*i) {					\
+			copy->map[*i] = xmalloc(sizeof(UidGidExtent));			\
+			if (!copy->map)							\
+				goto out;						\
+			memcpy(copy->map[*i], orig->map[*i], sizeof(UidGidExtent));	\
+		}									\
+	} while (0)
+
+#define FREE_MAP(map, n_map)								\
+	do {										\
+		if (copy->map) {							\
+			i = &copy->n_map;						\
+			while (--*i > 0)						\
+				xfree(copy->map[*i]);					\
+			xfree(copy->map);						\
+		}									\
+	} while (0)
+
+	COPY_MAP(uid_map, n_uid_map);
+	COPY_MAP(gid_map, n_gid_map);
+	ret = 0;
+out:
+	if (ret) {
+		pr_err("Can't dup entry\n");
+		if (copy) {
+			FREE_MAP(uid_map, n_uid_map);
+			FREE_MAP(gid_map, n_gid_map);
+			xfree(copy);
+		}
+		return NULL;
+	}
+	return copy;
+}
+
 const struct fdtype_ops nsfile_dump_ops = {
 	.type		= FD_TYPES__NS,
 	.dump		= dump_one_ns_file,



More information about the CRIU mailing list