This also introduces cpu_set_done_and_replace().
No functional change, just refactoring.
c->restrict_network_interfaces = set_free(c->restrict_network_interfaces);
- cpu_set_reset(&c->cpuset_cpus);
- cpu_set_reset(&c->startup_cpuset_cpus);
- cpu_set_reset(&c->cpuset_mems);
- cpu_set_reset(&c->startup_cpuset_mems);
+ cpu_set_done(&c->cpuset_cpus);
+ cpu_set_done(&c->startup_cpuset_cpus);
+ cpu_set_done(&c->cpuset_mems);
+ cpu_set_done(&c->startup_cpuset_mems);
c->delegate_subgroup = mfree(c->delegate_subgroup);
} else if (STR_IN_SET(name, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
const void *a;
size_t n;
- _cleanup_(cpu_set_reset) CPUSet new_set = {};
+ _cleanup_(cpu_set_done) CPUSet new_set = {};
r = sd_bus_message_read_array(message, 'y', &a, &n);
if (r < 0)
set = &c->cpuset_mems;
else if (streq(name, "StartupAllowedMemoryNodes"))
set = &c->startup_cpuset_mems;
+ else
+ assert_not_reached();
- assert(set);
-
- cpu_set_reset(set);
- *set = new_set;
- new_set = (CPUSet) {};
+ cpu_set_done_and_replace(*set, new_set);
unit_invalidate_cgroup(u, CGROUP_MASK_CPUSET);
unit_write_settingf(u, flags, name, "%s=\n%s=%s", name, name, setstr);
sd_bus_error *error) {
ExecContext *c = ASSERT_PTR(userdata);
- _cleanup_(cpu_set_reset) CPUSet s = {};
+ _cleanup_(cpu_set_done) CPUSet s = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
}
#endif
if (STR_IN_SET(name, "CPUAffinity", "NUMAMask")) {
+ _cleanup_(cpu_set_done) CPUSet set = {};
const void *a;
size_t n;
- bool affinity = streq(name, "CPUAffinity");
- _cleanup_(cpu_set_reset) CPUSet set = {};
r = sd_bus_message_read_array(message, 'y', &a, &n);
if (r < 0)
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+ CPUSet *cpuset = streq(name, "CPUAffinity") ? &c->cpu_set : &c->numa_policy.nodes;
+
if (n == 0) {
- cpu_set_reset(affinity ? &c->cpu_set : &c->numa_policy.nodes);
+ cpu_set_done(cpuset);
unit_write_settingf(u, flags, name, "%s=", name);
} else {
_cleanup_free_ char *str = NULL;
/* We forego any optimizations here, and always create the structure using
* cpu_set_add_set(), because we don't want to care if the existing size we
* got over dbus is appropriate. */
- r = cpu_set_add_set(affinity ? &c->cpu_set : &c->numa_policy.nodes, &set);
+ r = cpu_set_add_set(cpuset, &set);
if (r < 0)
return r;
sd_bus_error *error) {
Unit *u = ASSERT_PTR(userdata);
- _cleanup_(cpu_set_reset) CPUSet cpus = {};
+ _cleanup_(cpu_set_done) CPUSet cpus = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
sd_bus_error *error) {
Unit *u = ASSERT_PTR(userdata);
- _cleanup_(cpu_set_reset) CPUSet mems = {};
+ _cleanup_(cpu_set_done) CPUSet mems = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
return 0;
}
- _cleanup_(cpu_set_reset) CPUSet s = {};
+ _cleanup_(cpu_set_done) CPUSet s = {};
r = numa_to_cpu_set(&c->numa_policy, &s);
if (r < 0)
return r;
}
if (context->cpu_affinity_from_numa || context->cpu_set.set) {
- _cleanup_(cpu_set_reset) CPUSet converted_cpu_set = {};
+ _cleanup_(cpu_set_done) CPUSet converted_cpu_set = {};
const CPUSet *cpu_set;
if (context->cpu_affinity_from_numa) {
c->n_temporary_filesystems = 0;
c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
- cpu_set_reset(&c->cpu_set);
+ cpu_set_done(&c->cpu_set);
numa_policy_reset(&c->numa_policy);
c->utmp_id = mfree(c->utmp_id);
if (streq(rvalue, "numa")) {
c->cpu_affinity_from_numa = true;
- cpu_set_reset(&c->cpu_set);
+ cpu_set_done(&c->cpu_set);
return 0;
}
arg_machine_id = (sd_id128_t) {};
arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE;
- cpu_set_reset(&arg_cpu_affinity);
+ cpu_set_done(&arg_cpu_affinity);
numa_policy_reset(&arg_numa_policy);
arg_random_seed = mfree(arg_random_seed);
static int effective_cpuset_build_json(sd_json_variant **ret, const char *name, void *userdata, const char *cpuset_name) {
Unit *u = ASSERT_PTR(userdata);
- _cleanup_(cpu_set_reset) CPUSet cpus = {};
+ _cleanup_(cpu_set_done) CPUSet cpus = {};
int r;
assert(ret);
if (r < 0)
return json_log(v, flags, r, "Failed to parse CPU set specification: %s", n);
- cpu_set_reset(&data->cpu_set);
- data->cpu_set = set;
-
- return 0;
+ return cpu_set_done_and_replace(data->cpu_set, set);
}
static int oci_cgroup_cpu(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
r = oci_dispatch(v, table, flags, &data);
if (r < 0) {
- cpu_set_reset(&data.cpu_set);
+ cpu_set_done(&data.cpu_set);
return r;
}
- cpu_set_reset(&s->cpu_set);
- s->cpu_set = data.cpu_set;
+ cpu_set_done_and_replace(s->cpu_set, data.cpu_set);
if (data.weight != UINT64_MAX) {
r = settings_allocate_properties(s);
strv_free(s->syscall_deny_list);
rlimit_free_all(s->rlimit);
free(s->hostname);
- cpu_set_reset(&s->cpu_set);
+ cpu_set_done(&s->cpu_set);
strv_free(s->bind_user);
strv_free(s->network_interfaces);
STATIC_DESTRUCTOR_REGISTER(arg_seccomp, seccomp_releasep);
#endif
STATIC_DESTRUCTOR_REGISTER(arg_credentials, machine_credential_context_done);
-STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_reset);
+STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_done);
STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_settings_filename, freep);
if (r < 0)
return log_error_errno(r, "Failed to parse CPU affinity mask %s: %m", optarg);
- cpu_set_reset(&arg_cpu_set);
- arg_cpu_set = cpuset;
+ cpu_set_done_and_replace(arg_cpu_set, cpuset);
arg_settings_mask |= SETTING_CPU_AFFINITY;
break;
}
if (!arg_settings_trusted)
log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
- else {
- cpu_set_reset(&arg_cpu_set);
- arg_cpu_set = TAKE_STRUCT(settings->cpu_set);
- }
+ else
+ cpu_set_done_and_replace(arg_cpu_set, settings->cpu_set);
}
if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
"StartupAllowedCPUs",
"AllowedMemoryNodes",
"StartupAllowedMemoryNodes")) {
- _cleanup_(cpu_set_reset) CPUSet cpuset = {};
+
+ _cleanup_(cpu_set_done) CPUSet cpuset = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
}
if (streq(field, "CPUAffinity")) {
- _cleanup_(cpu_set_reset) CPUSet cpuset = {};
+ _cleanup_(cpu_set_done) CPUSet cpuset = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
}
if (streq(field, "NUMAMask")) {
- _cleanup_(cpu_set_reset) CPUSet nodes = {};
+ _cleanup_(cpu_set_done) CPUSet nodes = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
return TAKE_PTR(str) ?: strdup("0");
}
+void cpu_set_done(CPUSet *c) {
+ assert(c);
+
+ if (c->set)
+ CPU_FREE(c->set);
+
+ *c = (CPUSet) {};
+}
+
CPUSet* cpu_set_free(CPUSet *c) {
if (!c)
return c;
- cpu_set_reset(c);
+ cpu_set_done(c);
return mfree(c);
}
unsigned line,
const char *lvalue) {
- _cleanup_(cpu_set_reset) CPUSet c = {};
+ _cleanup_(cpu_set_done) CPUSet c = {};
const char *p = ASSERT_PTR(rvalue);
assert(cpu_set);
unsigned line,
const char *lvalue) {
- _cleanup_(cpu_set_reset) CPUSet cpuset = {};
+ _cleanup_(cpu_set_done) CPUSet cpuset = {};
int r;
assert(old);
return r;
if (!cpuset.set) {
- /* An empty assignment resets the CPU list */
- cpu_set_reset(old);
+ /* An empty assignment clears the CPU list */
+ cpu_set_done(old);
return 0;
}
}
int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *ret) {
- _cleanup_(cpu_set_reset) CPUSet c = {};
+ _cleanup_(cpu_set_done) CPUSet c = {};
int r;
assert(bits || size == 0);
size_t allocated; /* in bytes */
} CPUSet;
-static inline void cpu_set_reset(CPUSet *a) {
- assert((a->allocated > 0) == !!a->set);
- if (a->set)
- CPU_FREE(a->set);
- *a = (CPUSet) {};
-}
+void cpu_set_done(CPUSet *c);
+#define cpu_set_done_and_replace(a, b) \
+ ({ \
+ CPUSet *_a = &(a), *_b = &(b); \
+ cpu_set_done(_a); \
+ *_a = TAKE_STRUCT(*_b); \
+ 0; \
+ })
CPUSet* cpu_set_free(CPUSet *c);
DEFINE_TRIVIAL_CLEANUP_FUNC(CPUSet*, cpu_set_free);
}
int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) {
+ _cleanup_(cpu_set_done) CPUSet s = {};
int r;
- size_t i;
- _cleanup_(cpu_set_reset) CPUSet s = {};
assert(policy);
assert(ret);
- for (i = 0; i < policy->nodes.allocated * 8; i++) {
+ for (size_t i = 0; i < policy->nodes.allocated * 8; i++) {
_cleanup_free_ char *l = NULL;
char p[STRLEN("/sys/devices/system/node/node//cpulist") + DECIMAL_STR_MAX(size_t) + 1];
- _cleanup_(cpu_set_reset) CPUSet part = {};
if (!CPU_ISSET_S(i, policy->nodes.allocated, policy->nodes.set))
continue;
if (r < 0)
return r;
+ _cleanup_(cpu_set_done) CPUSet part = {};
r = parse_cpu_set(l, &part);
if (r < 0)
return r;
static inline void numa_policy_reset(NUMAPolicy *p) {
assert(p);
- cpu_set_reset(&p->nodes);
+ cpu_set_done(&p->nodes);
p->type = -1;
}
"EffectiveCPUs", "EffectiveMemoryNodes")) {
_cleanup_free_ char *affinity = NULL;
- _cleanup_(cpu_set_reset) CPUSet set = {};
+ _cleanup_(cpu_set_done) CPUSet set = {};
const void *a;
size_t n;
/* empty */
ASSERT_CPUSET_EMPTY(c);
ASSERT_CPUSET_STRING(c, "", "", "0");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Single value */
ASSERT_OK(parse_cpu_set("0", &c));
ASSERT_CPUSET_COUNT(c, 1);
ASSERT_CPUSET_ISSET(c, 0);
ASSERT_CPUSET_STRING(c, "0", "0", "1");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Simple range (from CPUAffinity example) */
ASSERT_OK(parse_cpu_set("1 2 4", &c));
ASSERT_CPUSET_ISSET(c, 2);
ASSERT_CPUSET_ISSET(c, 4);
ASSERT_CPUSET_STRING(c, "1 2 4", "1-2 4", "16");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* A more interesting range */
ASSERT_OK(parse_cpu_set("0 1 2 3 8 9 10 11", &c));
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Quoted strings */
ASSERT_OK(parse_cpu_set("8 '9' 10 \"11\"", &c));
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "8 9 10 11", "8-11", "f00");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Use commas as separators */
ASSERT_OK(parse_cpu_set("0,1,2,3 8,9,10,11", &c));
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Commas with spaces (and trailing comma, space) */
ASSERT_OK(parse_cpu_set("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c));
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_ISSET(c, 63);
ASSERT_CPUSET_STRING(c, "0 1 2 3 4 5 6 7 63", "0-7 63", "80000000,000000ff");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Ranges */
ASSERT_OK(parse_cpu_set("0-3,8-11", &c));
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
ASSERT_OK(parse_cpu_set("36-39,44-47", &c));
ASSERT_CPUSET_COUNT(c, 8);
for (unsigned i = 44; i < 48; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "36 37 38 39 44 45 46 47", "36-39 44-47", "f0f0,00000000");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
ASSERT_OK(parse_cpu_set("64-71", &c));
ASSERT_CPUSET_COUNT(c, 8);
for (unsigned i = 64; i < 72; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "64 65 66 67 68 69 70 71", "64-71", "ff,00000000,00000000");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Ranges with trailing comma, space */
ASSERT_OK(parse_cpu_set("0-3 8-11, ", &c));
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Negative range (returns empty cpu_set) */
ASSERT_OK(parse_cpu_set("3-0", &c));
ASSERT_CPUSET_COUNT(c, 0);
ASSERT_CPUSET_STRING(c, "", "", "0");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Overlapping ranges */
ASSERT_OK(parse_cpu_set("0-7 4-11", &c));
for (unsigned i = 0; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 4 5 6 7 8 9 10 11", "0-11", "fff");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Mix ranges and individual CPUs */
ASSERT_OK(parse_cpu_set("0,2 4-11", &c));
for (unsigned i = 4; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 2 4 5 6 7 8 9 10 11", "0 2 4-11", "ff5");
- cpu_set_reset(&c);
+ cpu_set_done(&c);
/* Garbage */
ASSERT_ERROR(parse_cpu_set("0 1 2 3 garbage", &c), EINVAL);
ASSERT_NOT_NULL(strextend_with_separator(&expected_mask, ",", i < 6 ? "ffffffff" : "00000000"));
ASSERT_CPUSET_STRING(c, expected_str, "8000-8191", expected_mask);
- cpu_set_reset(&c);
+ cpu_set_done(&c);
}
TEST(parse_cpu_set_extend) {
}
TEST(cpu_set_to_from_dbus) {
- _cleanup_(cpu_set_reset) CPUSet c = {}, c2 = {};
+ _cleanup_(cpu_set_done) CPUSet c = {}, c2 = {};
ASSERT_OK(parse_cpu_set("1 3 8 100-200", &c));
ASSERT_CPUSET_COUNT(c, 104);
}
static void test_exec_cpuaffinity(Manager *m) {
- _cleanup_(cpu_set_reset) CPUSet c = {};
+ _cleanup_(cpu_set_done) CPUSet c = {};
ASSERT_OK(cpu_set_realloc(&c, 8192)); /* just allocate the maximum possible size */
ASSERT_OK_ERRNO(sched_getaffinity(0, c.allocated, c.set));
}
if (streq(rvalue, "disable"))
- cpu_set_reset(mask);
+ cpu_set_done(mask);
else if (streq(rvalue, "all")) {
r = cpu_set_add_all(mask);