Avoid nesting and improve readability. Signed-off-by: David Ward <david.w...@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 ------------------------------------------------------------------------------ Try before you buy = See our experts in action! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-dev2 _______________________________________________ Lxc-devel mailing list Lxc-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/lxc-devel