[CRIU] [PATCH 3/7] namespace: Add parse_ns_proc helper

Cyrill Gorcunov gorcunov at openvz.org
Wed May 8 09:00:37 EDT 2013


It will help to parse procfs/ns links. Among other
things we do check for ns names being known when
we parse it.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/namespaces.h | 25 +++++++++++++++++++++++++
 namespaces.c         | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/include/namespaces.h b/include/namespaces.h
index c0a1ff7..bfe875c 100644
--- a/include/namespaces.h
+++ b/include/namespaces.h
@@ -4,6 +4,31 @@
 #include "crtools.h"
 #include "pstree.h"
 
+enum {
+	PROC_NS_NET,
+	PROC_NS_UTS,
+	PROC_NS_IPC,
+	PROC_NS_PID,
+	PROC_NS_USER,
+	PROC_NS_MNT,
+
+	PROC_NS_MAX
+};
+
+struct ns_proc_entry {
+	char		*name;
+	size_t		name_len;
+	unsigned long	i_ino;
+};
+
+#define NS_PROC_ENTRY(_index, _name)			\
+	[_index] = {					\
+		.name		= _name,		\
+		.name_len	= sizeof(_name) - 1,	\
+	}
+
+extern int parse_ns_proc(char *link, size_t len, struct ns_proc_entry *found);
+
 int dump_namespaces(struct pid *pid, unsigned int ns_flags);
 int prepare_namespace(int pid, unsigned long clone_flags);
 struct cr_options;
diff --git a/namespaces.c b/namespaces.c
index a182d40..f95928c 100644
--- a/namespaces.c
+++ b/namespaces.c
@@ -10,6 +10,41 @@
 #include "namespaces.h"
 #include "net.h"
 
+static struct ns_proc_entry ns_proc_entries[PROC_NS_MAX] = {
+	NS_PROC_ENTRY(PROC_NS_NET,	"net"),
+	NS_PROC_ENTRY(PROC_NS_UTS,	"uts"),
+	NS_PROC_ENTRY(PROC_NS_IPC,	"ipc"),
+	NS_PROC_ENTRY(PROC_NS_PID,	"pid"),
+	NS_PROC_ENTRY(PROC_NS_USER,	"user"),
+	NS_PROC_ENTRY(PROC_NS_MNT,	"mnt"),
+};
+
+int parse_ns_proc(char *link, size_t len, struct ns_proc_entry *found)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(ns_proc_entries); i++) {
+		size_t sz = ns_proc_entries[i].name_len;
+		char *end = NULL;
+
+		if (len < sz + 2)
+			continue;
+
+		if (link[sz] == ':' && !memcmp(link, ns_proc_entries[i].name, sz)) {
+			found->i_ino	= strtoul(&link[sz + 2], &end, 10);
+			found->name	= ns_proc_entries[i].name;
+			found->name_len	= sz;
+
+			if (end && *end == ']')
+				return 0;
+			else
+				return -EINVAL;
+		}
+	}
+
+	return -ENOENT;
+}
+
 int switch_ns(int pid, struct ns_desc *nd, int *rst)
 {
 	char buf[32];
-- 
1.8.1.4



More information about the CRIU mailing list