machined.c revision 1ddb263d21099ae42195c2bc382bdf72a7f24f82
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <pwd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <fcntl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/epoll.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "sd-daemon.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "strv.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "conf-parser.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "cgroup-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "mkdir.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "bus-util.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "bus-error.h"
c34255bdb217c2a1d3ac6348252437ab8be9ca46Lennart Poettering#include "label.h"
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering#include "machine-image.h"
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering#include "machined.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart PoetteringManager *manager_new(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Manager *m;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = new0(Manager, 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!m)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt m->machines = hashmap_new(&string_hash_ops);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt m->machine_units = hashmap_new(&string_hash_ops);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt m->machine_leaders = hashmap_new(NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering if (!m->machines || !m->machine_units || !m->machine_leaders) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering manager_free(m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
afc6adb5ec7e73bc13156c43f52fb015cd80cc68Lennart Poettering r = sd_event_default(&m->event);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering manager_free(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
cde93897cdefdd7c7f66c400a61e42ceee5f6a46Lennart Poettering sd_event_set_watchdog(m->event, true);
cde93897cdefdd7c7f66c400a61e42ceee5f6a46Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return m;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringvoid manager_free(Manager *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Machine *machine;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering Image *i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((machine = hashmap_first(m->machines)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine_free(machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_free(m->machines);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_free(m->machine_units);
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering hashmap_free(m->machine_leaders);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering while ((i = hashmap_steal_first(m->image_cache)))
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering image_unref(i);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering hashmap_free(m->image_cache);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering sd_event_source_unref(m->image_cache_defer_event);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering bus_verify_polkit_async_registry_free(m->polkit_registry);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sd_bus_unref(m->bus);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sd_event_unref(m->event);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering free(m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint manager_enumerate_machines(Manager *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct dirent *de;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Read in machine data stored on disk */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering d = opendir("/run/systemd/machines");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!d) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (errno == ENOENT)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -errno;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering FOREACH_DIRENT(de, d, return -errno) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct Machine *machine;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int k;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!dirent_is_file(de))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering /* Ignore symlinks that map the unit name to the machine */
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering if (startswith(de->d_name, "unit:"))
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering continue;
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering k = manager_add_machine(m, de->d_name, &machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (k < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = k;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine_add_to_gc_queue(machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering k = machine_load(machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (k < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = k;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int manager_connect_bus(Manager *m) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(!m->bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
76b543756ef69ce69784d571aefe8de65eaeb331Lennart Poettering r = sd_bus_default_system(&m->bus);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to connect to system bus: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add manager object vtable: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable, machine_object_find, m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add machine object vtable: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add machine enumerator: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable, image_object_find, m);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering if (r < 0)
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering return log_error_errno(r, "Failed to add image object vtable: %m");
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering if (r < 0)
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering return log_error_errno(r, "Failed to add image enumerator: %m");
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_add_match(m->bus,
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering NULL,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='JobRemoved',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "path='/org/freedesktop/systemd1'",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering match_job_removed,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for JobRemoved: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_add_match(m->bus,
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering NULL,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='UnitRemoved',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "path='/org/freedesktop/systemd1'",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering match_unit_removed,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for UnitRemoved: %m");
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_add_match(m->bus,
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering NULL,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.DBus.Properties',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='PropertiesChanged'",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering match_properties_changed,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_add_match(m->bus,
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering NULL,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='Reloading',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "path='/org/freedesktop/systemd1'",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering match_reloading,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for Reloading: %m");
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_call_method(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m->bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.systemd1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "/org/freedesktop/systemd1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.systemd1.Manager",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "Subscribe",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &error,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
5bb658a1784a0fd4f0f32adb4b1fb636ff503f7dKay Sievers r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", 0);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to register name: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_attach_event(m->bus, m->event, 0);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to attach bus to event loop: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringvoid manager_gc(Manager *m, bool drop_not_started) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Machine *machine;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((machine = m->machine_gc_queue)) {
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine->in_gc_queue = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (!machine_check_gc(machine, drop_not_started)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine_stop(machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine_free(machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint manager_startup(Manager *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Machine *machine;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Iterator i;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Connect to the bus */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = manager_connect_bus(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Deserialize state */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering manager_enumerate_machines(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Remove stale objects before we start them */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering manager_gc(m, false);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* And start everything */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering HASHMAP_FOREACH(machine, m->machines, i)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering machine_start(machine, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poetteringstatic bool check_idle(void *userdata) {
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering Manager *m = userdata;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering manager_gc(m, true);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering return hashmap_isempty(m->machines);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poetteringint manager_run(Manager *m) {
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering assert(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering return bus_event_loop_with_idle(
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering m->event,
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering m->bus,
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering "org.freedesktop.machine1",
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering DEFAULT_EXIT_USEC,
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering check_idle, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint main(int argc, char *argv[]) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Manager *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_set_target(LOG_TARGET_AUTO);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_set_facility(LOG_AUTH);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_parse_environment();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_open();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering umask(0022);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (argc != 1) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("This program takes no arguments.");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = -EINVAL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto finish;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Always create the directories people can create inotify
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * watches in. Note that some applications might check for the
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * existence of /run/systemd/machines/ to determine whether
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * machined is available, so please always make sure this
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering * check stays in. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering mkdir_label("/run/systemd/machines", 0755);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = manager_new();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = log_oom();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto finish;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = manager_startup(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to fully start up daemon: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto finish;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("systemd-machined running as pid "PID_FMT, getpid());
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_notify(false,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "READY=1\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "STATUS=Processing requests...");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = manager_run(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("systemd-machined stopped as pid "PID_FMT, getpid());
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringfinish:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (m)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering manager_free(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}