machined: optionally track machines in cgroup subgroups

This commit is contained in:
Lennart Poettering
2025-07-11 18:15:12 +02:00
parent 7bb1147b00
commit d5feeb373c
13 changed files with 110 additions and 38 deletions
+28 -13
View File
@@ -134,13 +134,19 @@ Machine* machine_free(Machine *m) {
sd_bus_message_unref(m->create_message);
free(m->name);
free(m->scope_job);
free(m->state_file);
free(m->service);
free(m->root_directory);
free(m->unit);
free(m->subgroup);
free(m->scope_job);
free(m->netif);
free(m->ssh_address);
free(m->ssh_private_key_path);
return mfree(m);
}
@@ -156,7 +162,7 @@ int machine_save(Machine *m) {
return 0;
_cleanup_(unlink_and_freep) char *sl = NULL; /* auto-unlink! */
if (m->unit) {
if (m->unit && !m->subgroup) {
sl = strjoin("/run/systemd/machines/unit:", m->unit);
if (!sl)
return log_oom();
@@ -244,7 +250,7 @@ int machine_save(Machine *m) {
static void machine_unlink(Machine *m) {
assert(m);
if (m->unit) {
if (m->unit && !m->subgroup) {
const char *sl = strjoina("/run/systemd/machines/unit:", m->unit);
(void) unlink(sl);
}
@@ -266,6 +272,7 @@ int machine_load(Machine *m) {
r = parse_env_file(NULL, m->state_file,
"NAME", &name,
"SCOPE", &m->unit,
"SUBGROUP", &m->subgroup,
"SCOPE_JOB", &m->scope_job,
"SERVICE", &m->service,
"ROOT", &m->root_directory,
@@ -380,6 +387,7 @@ static int machine_start_scope(
assert(machine);
assert(pidref_is_set(&machine->leader));
assert(!machine->unit);
assert(!machine->subgroup);
escaped = unit_name_escape(machine->name);
if (!escaped)
@@ -476,9 +484,11 @@ static int machine_ensure_scope(Machine *m, sd_bus_message *properties, sd_bus_e
assert(m->unit);
r = hashmap_ensure_put(&m->manager->machines_by_unit, &string_hash_ops, m->unit, m);
if (r < 0)
return r;
if (!m->subgroup) {
r = hashmap_ensure_put(&m->manager->machines_by_unit, &string_hash_ops, m->unit, m);
if (r < 0)
return r;
}
return 0;
}
@@ -566,7 +576,7 @@ int machine_stop(Machine *m) {
if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
return -EOPNOTSUPP;
if (m->unit) {
if (m->unit && !m->subgroup) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
char *job = NULL;
@@ -637,7 +647,7 @@ bool machine_may_gc(Machine *m, bool drop_not_started) {
return false;
}
if (m->unit) {
if (m->unit && !m->subgroup) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
r = manager_unit_is_active(m->manager, m->unit, &error);
@@ -683,14 +693,14 @@ int machine_kill(Machine *m, KillWhom whom, int signo) {
if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER))
return -EOPNOTSUPP;
if (!m->unit)
return -ESRCH;
if (whom == KILL_LEADER) /* If we shall simply kill the leader, do so directly */
return pidref_kill(&m->leader, signo);
if (!m->unit)
return -ESRCH;
/* Otherwise, make PID 1 do it for us, for the entire cgroup */
return manager_kill_unit(m->manager, m->unit, signo, NULL);
return manager_kill_unit(m->manager, m->unit, m->subgroup, signo, /* error= */ NULL);
}
int machine_openpt(Machine *m, int flags, char **ret_peer) {
@@ -1124,8 +1134,13 @@ void machine_release_unit(Machine *m) {
m->referenced = false;
}
(void) hashmap_remove_value(m->manager->machines_by_unit, m->unit, m);
if (!m->subgroup)
(void) hashmap_remove_value(m->manager->machines_by_unit, m->unit, m);
m->unit = mfree(m->unit);
/* Also free the subgroup, because it only makes sense in the context of the unit */
m->subgroup = mfree(m->subgroup);
}
int machine_get_uid_shift(Machine *m, uid_t *ret) {