[CRIU] [PATCH v3 3/6] ns: Pack functionality of storing ns fd to store_self_ns()
Kirill Tkhai
ktkhai at virtuozzo.com
Sat Apr 1 04:37:37 PDT 2017
Move the code to simplify it and to allow to use this function others.
Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
criu/include/namespaces.h | 1 +
criu/namespaces.c | 28 ++++++++++++++++------------
2 files changed, 17 insertions(+), 12 deletions(-)
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 fc75e5fa..b2348378 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -2159,6 +2159,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,
@@ -2179,7 +2193,7 @@ static int create_user_ns_hierarhy_fn(void *in_arg)
futex_t *p_futex = NULL, *futex = NULL;
size_t map_size = 2 * 1024 * 1024;
void *map = MAP_FAILED, *stack;
- int status, fd, ret = -1;
+ int status, ret = -1;
struct ns_id *me, *child;
pid_t pid = -1;
@@ -2191,17 +2205,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);
- /*
- * Note we can't use open_proc() here after
- * clone() with CLONE_FILES but no CLONE_VM.
- */
- 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;
More information about the CRIU
mailing list