[lxc-devel] [PATCH 5/6] Move common code to lxc_cgroup_create

Daniel Lezcano daniel.lezcano at free.fr
Wed Dec 15 16:49:19 UTC 2010


For both the ns_cgroup and the usual cgroup creation, we have to
check if a previous does not exist and remove it if it is empty.

Signed-off-by: Daniel Lezcano <daniel.lezcano at free.fr>
---
 src/lxc/cgroup.c |   82 +++++++++++++++++++++++++-----------------------------
 1 files changed, 38 insertions(+), 44 deletions(-)

diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index 4b54906..b711c64 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -85,71 +85,65 @@ out:
         return err;
 }
 
-int lxc_rename_nsgroup(const char *name, pid_t pid)
+int lxc_rename_nsgroup(const char *mnt, const char *name, pid_t pid)
 {
 	char oldname[MAXPATHLEN];
-	char newname[MAXPATHLEN];
-	char cgroup[MAXPATHLEN];
-	int ret;
 
-	if (get_cgroup_mount(MTAB, cgroup)) {
-		ERROR("cgroup is not mounted");
-		return -1;
-	}
+	snprintf(oldname, MAXPATHLEN, "%s/%d", mnt, pid);
 
-	snprintf(oldname, MAXPATHLEN, "%s/%d", cgroup, pid);
-	snprintf(newname, MAXPATHLEN, "%s/%s", cgroup, name);
-
-	/* there is a previous cgroup, assume it is empty, otherwise
-	 * that fails */
-	if (!access(newname, F_OK)) {
-		ret = rmdir(newname);
-		if (ret) {
-			SYSERROR("failed to remove previous cgroup '%s'",
-				 newname);
-			return ret;
-		}
+	if (rename(oldname, name)) {
+		SYSERROR("failed to rename cgroup %s->%s", oldname, name);
+		return -1;
 	}
 
-	ret = rename(oldname, newname);
-	if (ret)
-		SYSERROR("failed to rename cgroup %s->%s", oldname, newname);
-	else
-		DEBUG("'%s' renamed to '%s'", oldname, newname);
-
+	DEBUG("'%s' renamed to '%s'", oldname, name);
 
-	return ret;
+	return 0;
 }
 
-int lxc_unlink_nsgroup(const char *name)
+int lxc_cgroup_create(const char *name, pid_t pid)
 {
-	char nsgroup[MAXPATHLEN];
-	char cgroup[MAXPATHLEN];
-	int ret;
+	char cgmnt[MAXPATHLEN];
+	char cgname[MAXPATHLEN];
 
-	if (get_cgroup_mount(MTAB, cgroup)) {
+	if (get_cgroup_mount(MTAB, cgmnt)) {
 		ERROR("cgroup is not mounted");
 		return -1;
 	}
 
-	snprintf(nsgroup, MAXPATHLEN, "%s/%s", cgroup, name);
-	ret = rmdir(nsgroup);
-	if (ret)
-		SYSERROR("failed to remove cgroup '%s'", nsgroup);
-	else
-		DEBUG("'%s' unlinked", nsgroup);
+	snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name);
 
-	return ret;
-}
+	/*
+	 * There is a previous cgroup, assume it is empty,
+	 * otherwise that fails
+	 */
+	if (!access(cgname, F_OK) && rmdir(cgname)) {
+		SYSERROR("failed to remove previous cgroup '%s'", cgname);
+		return -1;
+	}
 
-int lxc_cgroup_create(const char *name, pid_t pid)
-{
-	return lxc_rename_nsgroup(name, pid);
+	return lxc_rename_nsgroup(cgmnt, cgname, pid);
 }
 
 int lxc_cgroup_destroy(const char *name)
 {
-	return lxc_unlink_nsgroup(name);
+	char cgmnt[MAXPATHLEN];
+	char cgname[MAXPATHLEN];
+
+	if (get_cgroup_mount(MTAB, cgmnt)) {
+		ERROR("cgroup is not mounted");
+		return -1;
+	}
+
+	snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name);
+	if (rmdir(cgmnt)) {
+		SYSERROR("failed to remove cgroup '%s'", cgname);
+		return -1;
+	}
+
+	DEBUG("'%s' unlinked", cgname);
+
+	return 0;
 }
 
 int lxc_cgroup_path_get(char **path, const char *name)
-- 
1.7.0.4





More information about the lxc-devel mailing list