cgroup: add missing OOM check, and shorten code a bit

cpu_set_to_range_string() can fail due to OOM. Handle that.

unit_write_settingf() exists, use it instead of formatting a string
beforehand.

cpu_set_add_all() can fail due to OOM. Let's avoid it if we don't have
to use it, just copy over the cpuset directly.
This commit is contained in:
Lennart Poettering
2019-11-01 10:22:03 +01:00
parent c259ac9aa2
commit a897a7b837

View File

@@ -926,23 +926,23 @@ int bus_cgroup_set_property(
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
_cleanup_free_ char *setstr = NULL;
_cleanup_free_ char *data = NULL;
CPUSet *set;
setstr = cpu_set_to_range_string(&new_set);
if (!setstr)
return -ENOMEM;
if (streq(name, "AllowedCPUs"))
set = &c->cpuset_cpus;
else
set = &c->cpuset_mems;
if (asprintf(&data, "%s=%s", name, setstr) < 0)
return -ENOMEM;
cpu_set_reset(set);
cpu_set_add_all(set, &new_set);
*set = new_set;
new_set = (CPUSet) {};
unit_invalidate_cgroup(u, CGROUP_MASK_CPUSET);
unit_write_setting(u, flags, name, data);
unit_write_settingf(u, flags, name, "%s=%s", name, setstr);
}
return 1;