[CRIU] [PATCH 2/7] proc: Introduce helper for parsing /proc/$pid/cgroup file

Pavel Emelyanov xemul at parallels.com
Thu May 8 06:07:41 PDT 2014


Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 include/proc_parse.h | 19 +++++++++++++++++
 proc_parse.c         | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/include/proc_parse.h b/include/proc_parse.h
index a1dfd7a..09c9efd 100644
--- a/include/proc_parse.h
+++ b/include/proc_parse.h
@@ -178,4 +178,23 @@ extern int parse_threads(int pid, struct pid **_t, int *_n);
 
 extern int check_mnt_id(void);
 
+/*
+ * This struct describes a group controlled by one controller.
+ * The @name is the controller name or 'name=...' for named cgroups.
+ * The @path is the path from the hierarchy root.
+ */
+
+struct cg_ctl {
+	struct list_head l;
+	char *name;
+	char *path;
+};
+
+/*
+ * Returns the list of cg_ctl-s sorted by name
+ */
+
+extern int parse_task_cgroup(int pid, struct list_head *l, unsigned int *n);
+extern void put_ctls(struct list_head *);
+
 #endif /* __CR_PROC_PARSE_H__ */
diff --git a/proc_parse.c b/proc_parse.c
index 26686d4..05d6225 100644
--- a/proc_parse.c
+++ b/proc_parse.c
@@ -1482,3 +1482,62 @@ int parse_threads(int pid, struct pid **_t, int *_n)
 
 	return 0;
 }
+
+int parse_task_cgroup(int pid, struct list_head *retl, unsigned int *n)
+{
+	int ret = 0;
+	FILE *f;
+
+	f = fopen_proc(pid, "cgroup");
+	while (fgets(buf, BUF_SIZE, f)) {
+		struct cg_ctl *ncc, *cc;
+		char *name, *path, *e;
+
+		ret = -1;
+		ncc = xmalloc(sizeof(*cc));
+		if (!ncc)
+			goto err;
+
+		name = strchr(buf, ':') + 1;
+		path = strchr(name, ':');
+		e = strchr(name, '\n');
+		*path++ = '\0';
+		if (e)
+			*e = '\0';
+
+		ncc->name = xstrdup(name);
+		ncc->path = xstrdup(path);
+		if (!ncc->name || !ncc->name) {
+			xfree(ncc->name);
+			xfree(ncc->path);
+			xfree(ncc);
+			goto err;
+		}
+
+		list_for_each_entry(cc, retl, l)
+			if (strcmp(cc->name, name) >= 0)
+				break;
+
+		list_add_tail(&ncc->l, &cc->l);
+		(*n)++;
+	}
+
+	fclose(f);
+	return 0;
+
+err:
+	put_ctls(retl);
+	fclose(f);
+	return ret;
+}
+
+void put_ctls(struct list_head *l)
+{
+	struct cg_ctl *c, *n;
+
+	list_for_each_entry_safe(c, n, l, l) {
+		xfree(c->name);
+		xfree(c->path);
+		xfree(c);
+	}
+}
-- 
1.8.4.2


More information about the CRIU mailing list