]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: propagate error from machine_new
authorDavid Tardon <dtardon@redhat.com>
Wed, 30 Nov 2022 15:46:05 +0000 (16:46 +0100)
committerDavid Tardon <dtardon@redhat.com>
Wed, 14 Dec 2022 08:57:35 +0000 (09:57 +0100)
src/machine/machine.c
src/machine/machine.h
src/machine/machined-dbus.c
src/machine/machined.c

index ca43c977b00889e5632619a40e652a23a32b619a..c08a645814d00f53cd91c0f0f4252c40cb773a26 100644 (file)
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Machine*, machine_free);
 
-Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
+int machine_new(Manager *manager, 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
          * means as much as "we don't know yet", and that we'll figure
@@ -48,26 +50,28 @@ Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
 
         m = new0(Machine, 1);
         if (!m)
-                return NULL;
+                return -ENOMEM;
 
         m->name = strdup(name);
         if (!m->name)
-                return NULL;
+                return -ENOMEM;
 
         if (class != MACHINE_HOST) {
                 m->state_file = path_join("/run/systemd/machines", m->name);
                 if (!m->state_file)
-                        return NULL;
+                        return -ENOMEM;
         }
 
         m->class = class;
 
-        if (hashmap_put(manager->machines, m->name, m) < 0)
-                return NULL;
+        r = hashmap_put(manager->machines, m->name, m);
+        if (r < 0)
+                return r;
 
         m->manager = manager;
 
-        return TAKE_PTR(m);
+        *ret = TAKE_PTR(m);
+        return 0;
 }
 
 Machine* machine_free(Machine *m) {
index 5e0e529567a45c27ddff8e5405a58cee1f675570..54ebcb3b26cf5b0d989da3f2dd410f912b4a0efe 100644 (file)
@@ -66,7 +66,7 @@ struct Machine {
         LIST_FIELDS(Machine, gc_queue);
 };
 
-Machine* machine_new(Manager *manager, MachineClass class, const char *name);
+int machine_new(Manager *manager, MachineClass class, const char *name, Machine **ret);
 Machine* machine_free(Machine *m);
 bool machine_may_gc(Machine *m, bool drop_not_started);
 void machine_add_to_gc_queue(Machine *m);
index 56dd22d7573b031777d9807359d0e7d6a99963fe..3da639279d8c74c64b85972ee745a4653b876462 100644 (file)
@@ -1492,15 +1492,16 @@ int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
 
 int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
         Machine *machine;
+        int r;
 
         assert(m);
         assert(name);
 
         machine = hashmap_get(m->machines, name);
         if (!machine) {
-                machine = machine_new(m, _MACHINE_CLASS_INVALID, name);
-                if (!machine)
-                        return -ENOMEM;
+                r = machine_new(m, _MACHINE_CLASS_INVALID, name, &machine);
+                if (r < 0)
+                        return r;
         }
 
         if (_machine)
index 9ecba8720fdd81c9e26bf30a4c009ab63d921f63..b4ff97ab701189e80a8a8e93f6d733b7f7b78d24 100644 (file)
@@ -117,9 +117,9 @@ static int manager_add_host_machine(Manager *m) {
         if (!unit)
                 return log_oom();
 
-        t = machine_new(m, MACHINE_HOST, ".host");
-        if (!t)
-                return log_oom();
+        r = machine_new(m, MACHINE_HOST, ".host", &t);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create machine: %m");
 
         t->leader = 1;
         t->id = mid;