From: Sam Leonard Date: Wed, 24 Apr 2024 08:57:18 +0000 (+0100) Subject: machined: split manager linking out of machine_new into machine_link X-Git-Tag: v256-rc2~71^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=885317f1aba9754856c3047277413004622c79a4;p=thirdparty%2Fsystemd.git machined: split manager linking out of machine_new into machine_link --- diff --git a/src/machine/machine.c b/src/machine/machine.c index d1ebd910b3e..2b71ae99661 100644 --- a/src/machine/machine.c +++ b/src/machine/machine.c @@ -30,19 +30,17 @@ #include "special.h" #include "stdio-util.h" #include "string-table.h" +#include "string-util.h" #include "terminal-util.h" #include "tmpfile-util.h" #include "uid-range.h" #include "unit-name.h" #include "user-util.h" -int machine_new(Manager *manager, MachineClass class, const char *name, Machine **ret) { +int machine_new(MachineClass class, const char *name, Machine **ret) { _cleanup_(machine_freep) Machine *m = NULL; - int r; - assert(manager); assert(class < _MACHINE_CLASS_MAX); - assert(name); assert(ret); /* Passing class == _MACHINE_CLASS_INVALID here is fine. It @@ -57,25 +55,43 @@ int machine_new(Manager *manager, MachineClass class, const char *name, Machine .leader = PIDREF_NULL, }; - m->name = strdup(name); - if (!m->name) - return -ENOMEM; - - if (class != MACHINE_HOST) { - m->state_file = path_join("/run/systemd/machines", m->name); - if (!m->state_file) + if (name) { + m->name = strdup(name); + if (!m->name) return -ENOMEM; } m->class = class; - r = hashmap_put(manager->machines, m->name, m); + *ret = TAKE_PTR(m); + return 0; +} + +int machine_link(Manager *manager, Machine *machine) { + int r; + + assert(manager); + assert(machine); + + if (machine->manager) + return -EEXIST; + if (!machine->name) + return -EINVAL; + + if (machine->class != MACHINE_HOST) { + char *temp = path_join("/run/systemd/machines", machine->name); + if (!temp) + return -ENOMEM; + + free_and_replace(machine->state_file, temp); + } + + r = hashmap_put(manager->machines, machine->name, machine); if (r < 0) return r; - m->manager = manager; + machine->manager = manager; - *ret = TAKE_PTR(m); return 0; } @@ -91,11 +107,9 @@ Machine* machine_free(Machine *m) { LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m); } - machine_release_unit(m); - - free(m->scope_job); - if (m->manager) { + machine_release_unit(m); + (void) hashmap_remove(m->manager->machines, m->name); if (m->manager->host_machine == m) @@ -111,6 +125,7 @@ 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); diff --git a/src/machine/machine.h b/src/machine/machine.h index 606f117d86f..24ef15894bf 100644 --- a/src/machine/machine.h +++ b/src/machine/machine.h @@ -67,7 +67,8 @@ struct Machine { LIST_FIELDS(Machine, gc_queue); }; -int machine_new(Manager *manager, MachineClass class, const char *name, Machine **ret); +int machine_new(MachineClass class, const char *name, Machine **ret); +int machine_link(Manager *manager, Machine *machine); Machine* machine_free(Machine *m); bool machine_may_gc(Machine *m, bool drop_not_started); void machine_add_to_gc_queue(Machine *m); diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index 6b108dc0094..e7c7f6fd695 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -1498,9 +1498,13 @@ int manager_add_machine(Manager *m, const char *name, Machine **_machine) { machine = hashmap_get(m->machines, name); if (!machine) { - r = machine_new(m, _MACHINE_CLASS_INVALID, name, &machine); + r = machine_new(_MACHINE_CLASS_INVALID, name, &machine); if (r < 0) return r; + + r = machine_link(m, machine); + if (r < 0) + return 0; } if (_machine) diff --git a/src/machine/machined.c b/src/machine/machined.c index 99301408d34..2638ed572ed 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -132,10 +132,14 @@ static int manager_add_host_machine(Manager *m) { if (r < 0) return log_error_errno(r, "Failed to open reference to PID 1: %m"); - r = machine_new(m, MACHINE_HOST, ".host", &t); + r = machine_new(MACHINE_HOST, ".host", &t); if (r < 0) return log_error_errno(r, "Failed to create machine: %m"); + r = machine_link(m, t); + if (r < 0) + return log_error_errno(r, "Failed to link machine to manager: %m"); + t->leader = TAKE_PIDREF(pidref); t->id = mid;