cgroup.c: clean up for handle_cgroup_settings

Clean up the nesting if, make the logic similar for memory
and cpuset, and the error message should sent from inside,
for better extendibility.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Qiang Huang 2014-01-20 16:37:06 +08:00 committed by Serge Hallyn
parent 55fc19a104
commit 032e28b767

View File

@ -848,10 +848,8 @@ struct cgroup_process_info *lxc_cgroupfs_create(const char *name, const char *pa
if (lxc_string_in_array("ns", (const char **)h->subsystems)) if (lxc_string_in_array("ns", (const char **)h->subsystems))
continue; continue;
if (handle_cgroup_settings(mp, info_ptr->cgroup_path) < 0) { if (handle_cgroup_settings(mp, info_ptr->cgroup_path) < 0)
ERROR("Could not set clone_children to 1 for cpuset hierarchy in parent cgroup.");
goto out_initial_error; goto out_initial_error;
}
} }
/* normalize the path */ /* normalize the path */
@ -2084,47 +2082,66 @@ static int handle_cgroup_settings(struct cgroup_mount_point *mp,
{ {
int r, saved_errno = 0; int r, saved_errno = 0;
char buf[2]; char buf[2];
const char **subsystems = (const char **)mp->hierarchy->subsystems;
/* If this is the memory cgroup, we want to enforce hierarchy. /* If this is the memory cgroup, we want to enforce hierarchy.
* But don't fail if for some reason we can't. * But don't fail if for some reason we can't.
*/ */
if (lxc_string_in_array("memory", (const char **)mp->hierarchy->subsystems)) { if (lxc_string_in_array("memory", subsystems)) {
char *cc_path = cgroup_to_absolute_path(mp, cgroup_path, "/memory.use_hierarchy"); char *cc_path = cgroup_to_absolute_path(mp, cgroup_path,
if (cc_path) { "/memory.use_hierarchy");
r = lxc_read_from_file(cc_path, buf, 1); if (!cc_path)
if (r < 1 || buf[0] != '1') { goto cpuset;
r = lxc_write_to_file(cc_path, "1", 1, false);
if (r < 0) r = lxc_read_from_file(cc_path, buf, 1);
SYSERROR("failed to set memory.use_hiararchy to 1; continuing"); if (r == 1 && buf[0] == '1') {
}
free(cc_path); free(cc_path);
goto cpuset;
} }
}
r = lxc_write_to_file(cc_path, "1", 1, false);
if (r < 0)
SYSERROR("Failed to set memory.use_hiararchy to 1; continuing");
free(cc_path);
}
/* if this is a cpuset hierarchy, we have to set cgroup.clone_children in /* if this is a cpuset hierarchy, we have to set cgroup.clone_children in
* the base cgroup, otherwise containers will start with an empty cpuset.mems * the base cgroup, otherwise containers will start with an empty cpuset.mems
* and cpuset.cpus and then * and cpuset.cpus and then
*/ */
if (lxc_string_in_array("cpuset", (const char **)mp->hierarchy->subsystems)) { cpuset:
char *cc_path = cgroup_to_absolute_path(mp, cgroup_path, "/cgroup.clone_children"); if (lxc_string_in_array("cpuset", subsystems)) {
struct stat sb; struct stat sb;
char *cc_path = cgroup_to_absolute_path(mp, cgroup_path,
"/cgroup.clone_children");
if (!cc_path) if (!cc_path)
return -1; goto err;
if (stat(cc_path, &sb) != 0 && errno == ENOENT) if (stat(cc_path, &sb) != 0 && errno == ENOENT) {
return 0; free(cc_path);
goto out;
}
r = lxc_read_from_file(cc_path, buf, 1); r = lxc_read_from_file(cc_path, buf, 1);
if (r == 1 && buf[0] == '1') { if (r == 1 && buf[0] == '1') {
free(cc_path); free(cc_path);
return 0; goto out;
} }
r = lxc_write_to_file(cc_path, "1", 1, false); r = lxc_write_to_file(cc_path, "1", 1, false);
saved_errno = errno; if (r < 0) {
free(cc_path); SYSERROR("Failed to set clone_children to 1 for cpuset hierarchy in parent cgroup.");
errno = saved_errno; saved_errno = errno;
return r < 0 ? -1 : 0; free(cc_path);
errno = saved_errno;
goto err;
}
free(cc_path);
} }
out:
return 0; return 0;
err:
return -1;
} }
extern void lxc_monitor_send_state(const char *name, lxc_state_t state, extern void lxc_monitor_send_state(const char *name, lxc_state_t state,