]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/machine/machine.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / machine / machine.c
index 0ed18d74ece89a8c0680e6f70b73a79ba0c74728..7aff55b761d9f1db2ebaf0fdacff10047b01f049 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <errno.h>
 #include <string.h>
 #include <unistd.h>
-#include <errno.h>
 
 #include "sd-messages.h"
 
-#include "util.h"
-#include "mkdir.h"
-#include "hashmap.h"
-#include "strv.h"
+#include "bus-error.h"
+#include "bus-util.h"
+#include "escape.h"
+#include "fd-util.h"
 #include "fileio.h"
+#include "formats-util.h"
+#include "hashmap.h"
+#include "machine-dbus.h"
+#include "machine.h"
+#include "mkdir.h"
+#include "parse-util.h"
 #include "special.h"
+#include "terminal-util.h"
 #include "unit-name.h"
-#include "machine.h"
-#include "bus-util.h"
-#include "bus-error.h"
+#include "util.h"
 
-Machine* machine_new(Manager *manager, const char *name) {
+Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
         Machine *m;
 
         assert(manager);
+        assert(class < _MACHINE_CLASS_MAX);
         assert(name);
 
+        /* Passing class == _MACHINE_CLASS_INVALID here is fine. It
+         * means as much as "we don't know yet", and that we'll figure
+         * it out later when loading the state file. */
+
         m = new0(Machine, 1);
         if (!m)
                 return NULL;
@@ -50,14 +60,17 @@ Machine* machine_new(Manager *manager, const char *name) {
         if (!m->name)
                 goto fail;
 
-        m->state_file = strappend("/run/systemd/machines/", m->name);
-        if (!m->state_file)
-                goto fail;
+        if (class != MACHINE_HOST) {
+                m->state_file = strappend("/run/systemd/machines/", m->name);
+                if (!m->state_file)
+                        goto fail;
+        }
+
+        m->class = class;
 
         if (hashmap_put(manager->machines, m->name, m) < 0)
                 goto fail;
 
-        m->class = _MACHINE_CLASS_INVALID;
         m->manager = manager;
 
         return m;
@@ -73,20 +86,23 @@ fail:
 void machine_free(Machine *m) {
         assert(m);
 
+        while (m->operations)
+                machine_operation_unref(m->operations);
+
         if (m->in_gc_queue)
                 LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m);
 
-        if (m->unit) {
-                hashmap_remove(m->manager->machine_units, m->unit);
-                free(m->unit);
-        }
+        machine_release_unit(m);
 
         free(m->scope_job);
 
-        hashmap_remove(m->manager->machines, m->name);
+        (void) hashmap_remove(m->manager->machines, m->name);
+
+        if (m->manager->host_machine == m)
+                m->manager->host_machine = NULL;
 
         if (m->leader > 0)
-                hashmap_remove_value(m->manager->machine_leaders, UINT_TO_PTR(m->leader), m);
+                (void) hashmap_remove_value(m->manager->machine_leaders, UINT_TO_PTR(m->leader), m);
 
         sd_bus_message_unref(m->create_message);
 
@@ -104,20 +120,22 @@ int machine_save(Machine *m) {
         int r;
 
         assert(m);
-        assert(m->state_file);
+
+        if (!m->state_file)
+                return 0;
 
         if (!m->started)
                 return 0;
 
         r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
         if (r < 0)
-                goto finish;
+                goto fail;
 
         r = fopen_temporary(m->state_file, &f, &temp_path);
         if (r < 0)
-                goto finish;
+                goto fail;
 
-        fchmod(fileno(f), 0644);
+        (void) fchmod(fileno(f), 0644);
 
         fprintf(f,
                 "# This is private data. Do not parse.\n"
@@ -130,7 +148,7 @@ int machine_save(Machine *m) {
                 escaped = cescape(m->unit);
                 if (!escaped) {
                         r = -ENOMEM;
-                        goto finish;
+                        goto fail;
                 }
 
                 fprintf(f, "SCOPE=%s\n", escaped); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */
@@ -145,7 +163,7 @@ int machine_save(Machine *m) {
                 escaped = cescape(m->service);
                 if (!escaped) {
                         r = -ENOMEM;
-                        goto finish;
+                        goto fail;
                 }
                 fprintf(f, "SERVICE=%s\n", escaped);
         }
@@ -156,7 +174,7 @@ int machine_save(Machine *m) {
                 escaped = cescape(m->root_directory);
                 if (!escaped) {
                         r = -ENOMEM;
-                        goto finish;
+                        goto fail;
                 }
                 fprintf(f, "ROOT=%s\n", escaped);
         }
@@ -194,11 +212,11 @@ int machine_save(Machine *m) {
 
         r = fflush_and_check(f);
         if (r < 0)
-                goto finish;
+                goto fail;
 
         if (rename(temp_path, m->state_file) < 0) {
                 r = -errno;
-                goto finish;
+                goto fail;
         }
 
         if (m->unit) {
@@ -206,20 +224,20 @@ int machine_save(Machine *m) {
 
                 /* Create a symlink from the unit name to the machine
                  * name, so that we can quickly find the machine for
-                 * each given unit */
-                sl = strappenda("/run/systemd/machines/unit:", m->unit);
-                symlink(m->name, sl);
+                 * each given unit. Ignore error. */
+                sl = strjoina("/run/systemd/machines/unit:", m->unit);
+                (void) symlink(m->name, sl);
         }
 
-finish:
-        if (r < 0) {
-                if (temp_path)
-                        unlink(temp_path);
+        return 0;
 
-                log_error("Failed to save machine data %s: %s", m->state_file, strerror(-r));
-        }
+fail:
+        (void) unlink(m->state_file);
 
-        return r;
+        if (temp_path)
+                (void) unlink(temp_path);
+
+        return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
 }
 
 static void machine_unlink(Machine *m) {
@@ -229,12 +247,12 @@ static void machine_unlink(Machine *m) {
 
                 char *sl;
 
-                sl = strappenda("/run/systemd/machines/unit:", m->unit);
-                unlink(sl);
+                sl = strjoina("/run/systemd/machines/unit:", m->unit);
+                (void) unlink(sl);
         }
 
         if (m->state_file)
-                unlink(m->state_file);
+                (void) unlink(m->state_file);
 }
 
 int machine_load(Machine *m) {
@@ -243,6 +261,9 @@ int machine_load(Machine *m) {
 
         assert(m);
 
+        if (!m->state_file)
+                return 0;
+
         r = parse_env_file(m->state_file, NEWLINE,
                            "SCOPE",     &m->unit,
                            "SCOPE_JOB", &m->scope_job,
@@ -259,8 +280,7 @@ int machine_load(Machine *m) {
                 if (r == -ENOENT)
                         return 0;
 
-                log_error("Failed to read %s: %s", m->state_file, strerror(-r));
-                return r;
+                return log_error_errno(r, "Failed to read %s: %m", m->state_file);
         }
 
         if (id)
@@ -291,14 +311,14 @@ int machine_load(Machine *m) {
 
         if (netif) {
                 size_t l, allocated = 0, nr = 0;
-                char *w, *state;
+                const char *word, *state;
                 int *ni = NULL;
 
-                FOREACH_WORD(w, l, netif, state) {
+                FOREACH_WORD(word, l, netif, state) {
                         char buf[l+1];
                         int ifi;
 
-                        *(char*) (mempcpy(buf, w, l)) = 0;
+                        *(char*) (mempcpy(buf, word, l)) = 0;
 
                         if (safe_atoi(buf, &ifi) < 0)
                                 continue;
@@ -325,6 +345,7 @@ static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_er
         int r = 0;
 
         assert(m);
+        assert(m->class != MACHINE_HOST);
 
         if (!m->unit) {
                 _cleanup_free_ char *escaped = NULL;
@@ -338,7 +359,7 @@ static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_er
                 if (!scope)
                         return log_oom();
 
-                description = strappenda(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name);
+                description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name);
 
                 r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job);
                 if (r < 0) {
@@ -364,6 +385,9 @@ int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
 
         assert(m);
 
+        if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
+                return -EOPNOTSUPP;
+
         if (m->started)
                 return 0;
 
@@ -377,10 +401,10 @@ int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
                 return r;
 
         log_struct(LOG_INFO,
-                   MESSAGE_ID(SD_MESSAGE_MACHINE_START),
+                   LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_START),
                    "NAME=%s", m->name,
                    "LEADER="PID_FMT, m->leader,
-                   "MESSAGE=New machine %s.", m->name,
+                   LOG_MESSAGE("New machine %s.", m->name),
                    NULL);
 
         if (!dual_timestamp_is_set(&m->timestamp))
@@ -402,16 +426,15 @@ static int machine_stop_scope(Machine *m) {
         int r;
 
         assert(m);
+        assert(m->class != MACHINE_HOST);
 
         if (!m->unit)
                 return 0;
 
-        if (!m->registered) {
-                r = manager_stop_unit(m->manager, m->unit, &error, &job);
-                if (r < 0) {
-                        log_error("Failed to stop machine scope: %s", bus_error_message(&error, r));
-                        return r;
-                }
+        r = manager_stop_unit(m->manager, m->unit, &error, &job);
+        if (r < 0) {
+                log_error("Failed to stop machine scope: %s", bus_error_message(&error, r));
+                return r;
         }
 
         free(m->scope_job);
@@ -421,36 +444,49 @@ static int machine_stop_scope(Machine *m) {
 }
 
 int machine_stop(Machine *m) {
-        int r = 0, k;
+        int r;
+        assert(m);
+
+        if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
+                return -EOPNOTSUPP;
+
+        r = machine_stop_scope(m);
+
+        m->stopping = true;
+
+        machine_save(m);
+
+        return r;
+}
+
+int machine_finalize(Machine *m) {
         assert(m);
 
         if (m->started)
                 log_struct(LOG_INFO,
-                           MESSAGE_ID(SD_MESSAGE_MACHINE_STOP),
+                           LOG_MESSAGE_ID(SD_MESSAGE_MACHINE_STOP),
                            "NAME=%s", m->name,
                            "LEADER="PID_FMT, m->leader,
-                           "MESSAGE=Machine %s terminated.", m->name,
+                           LOG_MESSAGE("Machine %s terminated.", m->name),
                            NULL);
 
-        /* Kill cgroup */
-        k = machine_stop_scope(m);
-        if (k < 0)
-                r = k;
-
         machine_unlink(m);
         machine_add_to_gc_queue(m);
 
-        if (m->started)
+        if (m->started) {
                 machine_send_signal(m, false);
+                m->started = false;
+        }
 
-        m->started = false;
-
-        return r;
+        return 0;
 }
 
 bool machine_check_gc(Machine *m, bool drop_not_started) {
         assert(m);
 
+        if (m->class == MACHINE_HOST)
+                return true;
+
         if (drop_not_started && !m->started)
                 return false;
 
@@ -476,8 +512,14 @@ void machine_add_to_gc_queue(Machine *m) {
 MachineState machine_get_state(Machine *s) {
         assert(s);
 
+        if (s->class == MACHINE_HOST)
+                return MACHINE_RUNNING;
+
+        if (s->stopping)
+                return MACHINE_CLOSING;
+
         if (s->scope_job)
-                return s->started ? MACHINE_OPENING : MACHINE_CLOSING;
+                return MACHINE_OPENING;
 
         return MACHINE_RUNNING;
 }
@@ -485,6 +527,9 @@ MachineState machine_get_state(Machine *s) {
 int machine_kill(Machine *m, KillWho who, int signo) {
         assert(m);
 
+        if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER))
+                return -EOPNOTSUPP;
+
         if (!m->unit)
                 return -ESRCH;
 
@@ -493,15 +538,98 @@ int machine_kill(Machine *m, KillWho who, int signo) {
 
                 if (kill(m->leader, signo) < 0)
                         return -errno;
+
+                return 0;
         }
 
         /* Otherwise make PID 1 do it for us, for the entire cgroup */
         return manager_kill_unit(m->manager, m->unit, signo, NULL);
 }
 
+int machine_openpt(Machine *m, int flags) {
+        assert(m);
+
+        switch (m->class) {
+
+        case MACHINE_HOST: {
+                int fd;
+
+                fd = posix_openpt(flags);
+                if (fd < 0)
+                        return -errno;
+
+                if (unlockpt(fd) < 0)
+                        return -errno;
+
+                return fd;
+        }
+
+        case MACHINE_CONTAINER:
+                if (m->leader <= 0)
+                        return -EINVAL;
+
+                return openpt_in_namespace(m->leader, flags);
+
+        default:
+                return -EOPNOTSUPP;
+        }
+}
+
+int machine_open_terminal(Machine *m, const char *path, int mode) {
+        assert(m);
+
+        switch (m->class) {
+
+        case MACHINE_HOST:
+                return open_terminal(path, mode);
+
+        case MACHINE_CONTAINER:
+                if (m->leader <= 0)
+                        return -EINVAL;
+
+                return open_terminal_in_namespace(m->leader, path, mode);
+
+        default:
+                return -EOPNOTSUPP;
+        }
+}
+
+MachineOperation *machine_operation_unref(MachineOperation *o) {
+        if (!o)
+                return NULL;
+
+        sd_event_source_unref(o->event_source);
+
+        safe_close(o->errno_fd);
+
+        if (o->pid > 1)
+                (void) kill(o->pid, SIGKILL);
+
+        sd_bus_message_unref(o->message);
+
+        if (o->machine) {
+                LIST_REMOVE(operations, o->machine->operations, o);
+                o->machine->n_operations--;
+        }
+
+        free(o);
+        return NULL;
+}
+
+void machine_release_unit(Machine *m) {
+        assert(m);
+
+        if (!m->unit)
+                return;
+
+        (void) hashmap_remove(m->manager->machine_units, m->unit);
+        m->unit = mfree(m->unit);
+}
+
 static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {
         [MACHINE_CONTAINER] = "container",
-        [MACHINE_VM] = "vm"
+        [MACHINE_VM] = "vm",
+        [MACHINE_HOST] = "host",
 };
 
 DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass);