]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/machine/machined.c
Merge pull request #1668 from ssahani/net1
[thirdparty/systemd.git] / src / machine / machined.c
index 9bfe2add54718905eb4bd0b25e74b4c8d4be5f93..fe229c88a8c8f039a51b53b61628952336c86e0f 100644 (file)
 #include <unistd.h>
 
 #include "sd-daemon.h"
-#include "cgroup-util.h"
-#include "bus-util.h"
+
 #include "bus-error.h"
-#include "label.h"
+#include "bus-util.h"
+#include "cgroup-util.h"
+#include "fd-util.h"
 #include "formats-util.h"
-#include "signal-util.h"
+#include "hostname-util.h"
+#include "label.h"
 #include "machine-image.h"
 #include "machined.h"
+#include "signal-util.h"
 
 Manager *manager_new(void) {
         Manager *m;
@@ -89,6 +92,45 @@ void manager_free(Manager *m) {
         free(m);
 }
 
+static int manager_add_host_machine(Manager *m) {
+        _cleanup_free_ char *rd = NULL, *unit = NULL;
+        sd_id128_t mid;
+        Machine *t;
+        int r;
+
+        if (m->host_machine)
+                return 0;
+
+        r = sd_id128_get_machine(&mid);
+        if (r < 0)
+                return log_error_errno(r, "Failed to get machine ID: %m");
+
+        rd = strdup("/");
+        if (!rd)
+                return log_oom();
+
+        unit = strdup("-.slice");
+        if (!unit)
+                return log_oom();
+
+        t = machine_new(m, MACHINE_HOST, ".host");
+        if (!t)
+                return log_oom();
+
+        t->leader = 1;
+        t->id = mid;
+
+        t->root_directory = rd;
+        t->unit = unit;
+        rd = unit = NULL;
+
+        dual_timestamp_from_boottime_or_monotonic(&t->timestamp, 0);
+
+        m->host_machine = t;
+
+        return 0;
+}
+
 int manager_enumerate_machines(Manager *m) {
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
@@ -96,6 +138,10 @@ int manager_enumerate_machines(Manager *m) {
 
         assert(m);
 
+        r = manager_add_host_machine(m);
+        if (r < 0)
+                return r;
+
         /* Read in machine data stored on disk */
         d = opendir("/run/systemd/machines");
         if (!d) {
@@ -117,11 +163,12 @@ int manager_enumerate_machines(Manager *m) {
                 if (startswith(de->d_name, "unit:"))
                         continue;
 
+                if (!machine_name_is_valid(de->d_name))
+                        continue;
+
                 k = manager_add_machine(m, de->d_name, &machine);
                 if (k < 0) {
-                        log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
-
-                        r = k;
+                        r = log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
                         continue;
                 }
 
@@ -247,8 +294,16 @@ void manager_gc(Manager *m, bool drop_not_started) {
                 LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
                 machine->in_gc_queue = false;
 
-                if (!machine_check_gc(machine, drop_not_started)) {
+                /* First, if we are not closing yet, initiate stopping */
+                if (!machine_check_gc(machine, drop_not_started) &&
+                    machine_get_state(machine) != MACHINE_CLOSING)
                         machine_stop(machine);
+
+                /* Now, the stop stop probably made this referenced
+                 * again, but if it didn't, then it's time to let it
+                 * go entirely. */
+                if (!machine_check_gc(machine, drop_not_started)) {
+                        machine_finalize(machine);
                         machine_free(machine);
                 }
         }
@@ -347,8 +402,7 @@ int main(int argc, char *argv[]) {
         log_debug("systemd-machined stopped as pid "PID_FMT, getpid());
 
 finish:
-        if (m)
-                manager_free(m);
+        manager_free(m);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }