[v3,16/33] ns: Implement dup_userns_entry()

Submitted by Kirill Tkhai on Feb. 16, 2017, 12:08 p.m.

Details

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

Commit Message

Kirill Tkhai Feb. 16, 2017, 12:08 p.m.
Function for cloning UsernsEntry entries.

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

Patch hide | download patch | download mbox

diff --git a/criu/namespaces.c b/criu/namespaces.c
index 0fafcfac7..d412cf388 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,