[lxc-devel] [PATCH 7/9] cgroup: rearrange code blocks

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


Avoid nesting and improve readability.

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

diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index cc3910a..c915b52 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -142,6 +142,7 @@ static int get_cgroup_mount(const char *subsystem, char *mnt)
 	struct mntent *mntent;
 	char initcgroup[MAXPATHLEN];
 	FILE *file = NULL;
+	int ret, err = -1;
 
 	file = setmntent(MTAB, "r");
 	if (!file) {
@@ -150,30 +151,27 @@ static int get_cgroup_mount(const char *subsystem, char *mnt)
 	}
 
 	while ((mntent = getmntent(file))) {
-
 		if (strcmp(mntent->mnt_type, "cgroup"))
 			continue;
-		if (!subsystem || hasmntopt_multiple(mntent, subsystem)) {
-			int ret;
-			ret = snprintf(mnt, MAXPATHLEN, "%s%s/lxc",
-				       mntent->mnt_dir,
-				       get_init_cgroup(subsystem, NULL,
-						       initcgroup));
-			if (ret < 0 || ret >= MAXPATHLEN)
-				goto fail;
-			fclose(file);
-			DEBUG("using cgroup mounted at '%s'", mnt);
-			return 0;
-		}
+		if (subsystem && !hasmntopt_multiple(mntent, subsystem))
+			continue;
+
+		ret = snprintf(mnt, MAXPATHLEN, "%s%s/lxc", mntent->mnt_dir,
+			       get_init_cgroup(subsystem, NULL, initcgroup));
+		if (ret < 0 || ret >= MAXPATHLEN)
+			goto fail;
+
+		DEBUG("using cgroup mounted at '%s'", mnt);
+		err = 0;
+		goto out;
 	};
 
 fail:
 	DEBUG("Failed to find cgroup for %s\n",
 	      subsystem ? subsystem : "(NULL)");
-
-	fclose(file);
-
-	return -1;
+out:
+	endmntent(file);
+	return err;
 }
 
 int lxc_ns_is_mounted(void)
@@ -398,18 +396,17 @@ int lxc_cgroup_create(const char *name, pid_t pid)
 	}
 
 	while ((mntent = getmntent(file))) {
-
 		DEBUG("checking '%s' (%s)", mntent->mnt_dir, mntent->mnt_type);
 
-		if (!strcmp(mntent->mnt_type, "cgroup")) {
+		if (strcmp(mntent->mnt_type, "cgroup"))
+			continue;
 
-			INFO("[%d] found cgroup mounted at '%s',opts='%s'",
-			     ++found, mntent->mnt_dir, mntent->mnt_opts);
+		INFO("[%d] found cgroup mounted at '%s',opts='%s'",
+		     ++found, mntent->mnt_dir, mntent->mnt_opts);
 
-			err = lxc_one_cgroup_create(name, mntent, pid);
-			if (err)
-				goto out;
-		}
+		err = lxc_one_cgroup_create(name, mntent, pid);
+		if (err)
+			goto out;
 	};
 
 	if (!found)
@@ -485,7 +482,7 @@ int lxc_cgroup_destroy(const char *name)
 {
 	struct mntent *mntent;
 	FILE *file = NULL;
-	int ret, err = -1;
+	int err = -1;
 
 	file = setmntent(MTAB, "r");
 	if (!file) {
@@ -494,18 +491,15 @@ int lxc_cgroup_destroy(const char *name)
 	}
 
 	while ((mntent = getmntent(file))) {
-		if (!strcmp(mntent->mnt_type, "cgroup")) {
-			ret = lxc_one_cgroup_destroy(mntent, name);
-			if (ret) {
-				fclose(file);
-				return ret;
-			}
-			err = 0;
-		}
-	}
+		if (strcmp(mntent->mnt_type, "cgroup"))
+			continue;
 
-	fclose(file);
+		err = lxc_one_cgroup_destroy(mntent, name);
+		if (err)
+			break;
+	}
 
+	endmntent(file);
 	return err;
 }
 /*
-- 
1.7.1





More information about the lxc-devel mailing list