cgroups: rework return values of some functions

parent 093469d7
...@@ -2887,8 +2887,7 @@ static void cg_unified_delegate(char ***delegate) ...@@ -2887,8 +2887,7 @@ static void cg_unified_delegate(char ***delegate)
/* At startup, parse_hierarchies finds all the info we need about cgroup /* At startup, parse_hierarchies finds all the info we need about cgroup
* mountpoints and current cgroups, and stores it in @d. * mountpoints and current cgroups, and stores it in @d.
*/ */
static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative, static int cg_hybrid_init(struct cgroup_ops *ops, bool relative, bool unprivileged)
bool unprivileged)
{ {
__do_free char *basecginfo = NULL; __do_free char *basecginfo = NULL;
__do_free char *line = NULL; __do_free char *line = NULL;
...@@ -2905,19 +2904,15 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative, ...@@ -2905,19 +2904,15 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative,
else else
basecginfo = read_file("/proc/self/cgroup"); basecginfo = read_file("/proc/self/cgroup");
if (!basecginfo) if (!basecginfo)
return false; return ret_set_errno(-1, ENOMEM);
ret = get_existing_subsystems(&klist, &nlist); ret = get_existing_subsystems(&klist, &nlist);
if (ret < 0) { if (ret < 0)
ERROR("Failed to retrieve available legacy cgroup controllers"); return log_error_errno(-1, errno, "Failed to retrieve available legacy cgroup controllers");
return false;
}
f = fopen("/proc/self/mountinfo", "r"); f = fopen("/proc/self/mountinfo", "r");
if (!f) { if (!f)
ERROR("Failed to open \"/proc/self/mountinfo\""); return log_error_errno(-1, errno, "Failed to open \"/proc/self/mountinfo\"");
return false;
}
lxc_cgfsng_print_basecg_debuginfo(basecginfo, klist, nlist); lxc_cgfsng_print_basecg_debuginfo(basecginfo, klist, nlist);
...@@ -2954,22 +2949,18 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative, ...@@ -2954,22 +2949,18 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative,
if (type == CGROUP_SUPER_MAGIC) if (type == CGROUP_SUPER_MAGIC)
if (controller_list_is_dup(ops->hierarchies, controller_list)) if (controller_list_is_dup(ops->hierarchies, controller_list))
goto next; log_trace_errno(goto next, EEXIST, "Skipping duplicating controller");
mountpoint = cg_hybrid_get_mountpoint(line); mountpoint = cg_hybrid_get_mountpoint(line);
if (!mountpoint) { if (!mountpoint)
ERROR("Failed parsing mountpoint from \"%s\"", line); log_error_errno(goto next, EINVAL, "Failed parsing mountpoint from \"%s\"", line);
goto next;
}
if (type == CGROUP_SUPER_MAGIC) if (type == CGROUP_SUPER_MAGIC)
base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, controller_list[0], CGROUP_SUPER_MAGIC); base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, controller_list[0], CGROUP_SUPER_MAGIC);
else else
base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, NULL, CGROUP2_SUPER_MAGIC); base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, NULL, CGROUP2_SUPER_MAGIC);
if (!base_cgroup) { if (!base_cgroup)
ERROR("Failed to find current cgroup"); log_error_errno(goto next, EINVAL, "Failed to find current cgroup");
goto next;
}
trim(base_cgroup); trim(base_cgroup);
prune_init_scope(base_cgroup); prune_init_scope(base_cgroup);
...@@ -2978,7 +2969,7 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative, ...@@ -2978,7 +2969,7 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative,
else else
writeable = test_writeable_v1(mountpoint, base_cgroup); writeable = test_writeable_v1(mountpoint, base_cgroup);
if (!writeable) if (!writeable)
goto next; log_trace_errno(goto next, EROFS, "The %s group is not writeable", base_cgroup);
if (type == CGROUP2_SUPER_MAGIC) { if (type == CGROUP2_SUPER_MAGIC) {
char *cgv2_ctrl_path; char *cgv2_ctrl_path;
...@@ -2998,7 +2989,7 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative, ...@@ -2998,7 +2989,7 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative,
/* Exclude all controllers that cgroup use does not want. */ /* Exclude all controllers that cgroup use does not want. */
if (!cgroup_use_wants_controllers(ops, controller_list)) if (!cgroup_use_wants_controllers(ops, controller_list))
goto next; log_trace_errno(goto next, EINVAL, "Skipping controller");
new = add_hierarchy(&ops->hierarchies, controller_list, mountpoint, base_cgroup, type); new = add_hierarchy(&ops->hierarchies, controller_list, mountpoint, base_cgroup, type);
if (type == CGROUP2_SUPER_MAGIC && !ops->unified) { if (type == CGROUP2_SUPER_MAGIC && !ops->unified) {
...@@ -3025,9 +3016,9 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative, ...@@ -3025,9 +3016,9 @@ static bool cg_hybrid_init(struct cgroup_ops *ops, bool relative,
* controllers are accounted for * controllers are accounted for
*/ */
if (!all_controllers_found(ops)) if (!all_controllers_found(ops))
return false; return log_error_errno(-1, ENOENT, "Failed to find all required controllers");
return true; return 0;
} }
/* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */ /* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */
...@@ -3114,7 +3105,7 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative, ...@@ -3114,7 +3105,7 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative,
return CGROUP2_SUPER_MAGIC; return CGROUP2_SUPER_MAGIC;
} }
static bool cg_init(struct cgroup_ops *ops, struct lxc_conf *conf) static int cg_init(struct cgroup_ops *ops, struct lxc_conf *conf)
{ {
int ret; int ret;
const char *tmp; const char *tmp;
...@@ -3134,29 +3125,32 @@ static bool cg_init(struct cgroup_ops *ops, struct lxc_conf *conf) ...@@ -3134,29 +3125,32 @@ static bool cg_init(struct cgroup_ops *ops, struct lxc_conf *conf)
ret = cg_unified_init(ops, relative, !lxc_list_empty(&conf->id_map)); ret = cg_unified_init(ops, relative, !lxc_list_empty(&conf->id_map));
if (ret < 0) if (ret < 0)
return false; return -1;
if (ret == CGROUP2_SUPER_MAGIC) if (ret == CGROUP2_SUPER_MAGIC)
return true; return 0;
return cg_hybrid_init(ops, relative, !lxc_list_empty(&conf->id_map)); return cg_hybrid_init(ops, relative, !lxc_list_empty(&conf->id_map));
} }
__cgfsng_ops static bool cgfsng_data_init(struct cgroup_ops *ops) __cgfsng_ops static int cgfsng_data_init(struct cgroup_ops *ops)
{ {
const char *cgroup_pattern; const char *cgroup_pattern;
if (!ops)
return ret_set_errno(-1, ENOENT);
/* copy system-wide cgroup information */ /* copy system-wide cgroup information */
cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern"); cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
if (!cgroup_pattern) { if (!cgroup_pattern) {
/* lxc.cgroup.pattern is only NULL on error. */ /* lxc.cgroup.pattern is only NULL on error. */
ERROR("Failed to retrieve cgroup pattern"); ERROR("Failed to retrieve cgroup pattern");
return false; return ret_set_errno(-1, ENOMEM);
} }
ops->cgroup_pattern = must_copy_string(cgroup_pattern); ops->cgroup_pattern = must_copy_string(cgroup_pattern);
ops->monitor_pattern = MONITOR_CGROUP; ops->monitor_pattern = MONITOR_CGROUP;
return true; return 0;
} }
struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf) struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf)
...@@ -3165,12 +3159,12 @@ struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf) ...@@ -3165,12 +3159,12 @@ struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf)
cgfsng_ops = malloc(sizeof(struct cgroup_ops)); cgfsng_ops = malloc(sizeof(struct cgroup_ops));
if (!cgfsng_ops) if (!cgfsng_ops)
return NULL; return ret_set_errno(NULL, ENOMEM);
memset(cgfsng_ops, 0, sizeof(struct cgroup_ops)); memset(cgfsng_ops, 0, sizeof(struct cgroup_ops));
cgfsng_ops->cgroup_layout = CGROUP_LAYOUT_UNKNOWN; cgfsng_ops->cgroup_layout = CGROUP_LAYOUT_UNKNOWN;
if (!cg_init(cgfsng_ops, conf)) if (cg_init(cgfsng_ops, conf))
return NULL; return NULL;
cgfsng_ops->unified_fd = -EBADF; cgfsng_ops->unified_fd = -EBADF;
......
...@@ -132,7 +132,7 @@ struct cgroup_ops { ...@@ -132,7 +132,7 @@ struct cgroup_ops {
*/ */
cgroup_layout_t cgroup_layout; cgroup_layout_t cgroup_layout;
bool (*data_init)(struct cgroup_ops *ops); int (*data_init)(struct cgroup_ops *ops);
void (*payload_destroy)(struct cgroup_ops *ops, struct lxc_handler *handler); void (*payload_destroy)(struct cgroup_ops *ops, struct lxc_handler *handler);
void (*monitor_destroy)(struct cgroup_ops *ops, struct lxc_handler *handler); void (*monitor_destroy)(struct cgroup_ops *ops, struct lxc_handler *handler);
bool (*monitor_create)(struct cgroup_ops *ops, struct lxc_handler *handler); bool (*monitor_create)(struct cgroup_ops *ops, struct lxc_handler *handler);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment