[CRIU] [PATCH 1/3] ns: Split __get_ns_id() in subfunction
Kirill Tkhai
ktkhai at virtuozzo.com
Wed Jun 22 07:19:05 PDT 2016
Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
criu/include/namespaces.h | 1 +
criu/namespaces.c | 50 ++++++++++++++++++++++++++-------------------
2 files changed, 30 insertions(+), 21 deletions(-)
diff --git a/criu/include/namespaces.h b/criu/include/namespaces.h
index 4cd2c85..da81031 100644
--- a/criu/include/namespaces.h
+++ b/criu/include/namespaces.h
@@ -138,6 +138,7 @@ extern int prepare_namespace(struct pstree_item *item, unsigned long clone_flags
extern int switch_ns(int pid, struct ns_desc *nd, int *rst);
extern int restore_ns(int rst, struct ns_desc *nd);
+extern int read_ns_id(pid_t pid, struct ns_desc *nd, unsigned int *kid);
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, enum ns_type t);
diff --git a/criu/namespaces.c b/criu/namespaces.c
index 04a1b11..3703349 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -386,6 +386,34 @@ int walk_namespaces(struct ns_desc *nd, int (*cb)(struct ns_id *, void *), void
return ret;
}
+int read_ns_id(pid_t pid, struct ns_desc *nd, unsigned int *kid)
+{
+ int proc_dir, ret;
+ char ns_path[10], ns_id[32];
+
+ proc_dir = open_pid_proc(pid);
+ if (proc_dir < 0)
+ return -1;
+
+ sprintf(ns_path, "ns/%s", nd->str);
+ ret = readlinkat(proc_dir, ns_path, ns_id, sizeof(ns_id) - 1);
+ if (ret < 0) {
+ if (errno == ENOENT) {
+ /* The namespace is unsupported */
+ *kid = 0;
+ return 0;
+ }
+ pr_perror("Can't readlink ns link");
+ return -1;
+ }
+ ns_id[ret] = '\0';
+
+ *kid = parse_ns_link(ns_id, ret, nd);
+ BUG_ON(!*kid);
+
+ return 0;
+}
+
static unsigned int generate_ns_id(int pid, unsigned int kid, struct ns_desc *nd,
struct ns_id **ns_ret)
{
@@ -428,31 +456,11 @@ static unsigned int generate_ns_id(int pid, unsigned int kid, struct ns_desc *nd
static unsigned int __get_ns_id(int pid, struct ns_desc *nd, protobuf_c_boolean *supported, struct ns_id **ns)
{
- int proc_dir, ret;
unsigned int kid;
- char ns_path[10], ns_id[32];
- proc_dir = open_pid_proc(pid);
- if (proc_dir < 0)
- return 0;
-
- sprintf(ns_path, "ns/%s", nd->str);
- ret = readlinkat(proc_dir, ns_path, ns_id, sizeof(ns_id) - 1);
- if (ret < 0) {
- if (errno == ENOENT) {
- /* The namespace is unsupported */
- kid = 0;
- goto out;
- }
- pr_perror("Can't readlink ns link");
+ if (read_ns_id(pid, nd, &kid) < 0)
return 0;
- }
- ns_id[ret] = '\0';
- kid = parse_ns_link(ns_id, ret, nd);
- BUG_ON(!kid);
-
-out:
if (supported)
*supported = kid != 0;
return generate_ns_id(pid, kid, nd, ns);
More information about the CRIU
mailing list