[lxc-devel] [PATCH 9/9] cgroup: only touch hierarchies that are bound to subsystems

David Ward david.ward at ll.mit.edu
Mon Mar 5 13:35:01 UTC 2012


Obtain a list of subsystems from /proc/cgroups, and ignore hierarchies
that are not bound to any of them (especially the 'systemd' hierarchy:
http://www.freedesktop.org/wiki/Software/systemd/PaxControlGroups ).

Signed-off-by: David Ward <david.ward at ll.mit.edu>
---
 src/lxc/cgroup.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index 06aa1a0..8ccbc50 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -53,6 +53,39 @@ enum {
 	CGROUP_CLONE_CHILDREN,
 };
 
+/* Check if a mount is a cgroup hierarchy for any subsystem.
+ * Return the first subsystem found (or NULL if none).
+ */
+static char *mount_has_subsystem(const struct mntent *mntent)
+{
+	FILE *f;
+	char *c, *ret;
+	char line[MAXPATHLEN];
+
+	/* read the list of subsystems from the kernel */
+	f = fopen("/proc/cgroups", "r");
+	if (!f)
+		return 0;
+
+	/* skip the first line, which contains column headings */
+	if (!fgets(line, MAXPATHLEN, f))
+		return 0;
+
+	while (fgets(line, MAXPATHLEN, f)) {
+		c = strchr(line, '\t');
+		if (!c)
+			continue;
+		*c = '\0';
+
+		ret = hasmntopt(mntent, line);
+		if (ret)
+			break;
+	}
+
+	fclose(f);
+	return ret;
+}
+
 /*
  * get_init_cgroup: get the cgroup init is in.
  *  dsg: preallocated buffer to put the output in
@@ -124,8 +157,15 @@ static int get_cgroup_mount(const char *subsystem, char *mnt)
 	while ((mntent = getmntent(file))) {
 		if (strcmp(mntent->mnt_type, "cgroup"))
 			continue;
-		if (subsystem && !hasmntopt(mntent, subsystem))
-			continue;
+
+		if (subsystem) {
+			if (!hasmntopt(mntent, subsystem))
+				continue;
+		}
+		else {
+			if (!mount_has_subsystem(mntent))
+				continue;
+		}
 
 		ret = snprintf(mnt, MAXPATHLEN, "%s%s/lxc", mntent->mnt_dir,
 			       get_init_cgroup(subsystem, NULL, initcgroup));
@@ -252,6 +292,8 @@ int lxc_cgroup_attach(const char *name, pid_t pid)
 
 		if (strcmp(mntent->mnt_type, "cgroup"))
 			continue;
+		if (!mount_has_subsystem(mntent))
+			continue;
 
 		INFO("[%d] found cgroup mounted at '%s',opts='%s'",
 		     ++found, mntent->mnt_dir, mntent->mnt_opts);
@@ -405,6 +447,8 @@ int lxc_cgroup_create(const char *name, pid_t pid)
 
 		if (strcmp(mntent->mnt_type, "cgroup"))
 			continue;
+		if (!mount_has_subsystem(mntent))
+			continue;
 
 		INFO("[%d] found cgroup mounted at '%s',opts='%s'",
 		     ++found, mntent->mnt_dir, mntent->mnt_opts);
@@ -502,6 +546,8 @@ int lxc_cgroup_destroy(const char *name)
 	while ((mntent = getmntent(file))) {
 		if (strcmp(mntent->mnt_type, "cgroup"))
 			continue;
+		if (!mount_has_subsystem(mntent))
+			continue;
 
 		err = lxc_one_cgroup_destroy(mntent, name);
 		if (err)
-- 
1.7.1





More information about the lxc-devel mailing list