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 <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "sd-daemon.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "bus-error.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "bus-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "cgroup-util.h"
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include "dirent-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "fd-util.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "formats-util.h"
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering#include "hostname-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "label.h"
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering#include "machine-image.h"
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering#include "machined.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "signal-util.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
fbe550738d03b178bb004a1390e74115e904118aLennart Poetteringstatic int manager_add_host_machine(Manager *m) {
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering _cleanup_free_ char *rd = NULL, *unit = NULL;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering sd_id128_t mid;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering Machine *t;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering int r;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering if (m->host_machine)
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return 0;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering r = sd_id128_get_machine(&mid);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering if (r < 0)
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return log_error_errno(r, "Failed to get machine ID: %m");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering rd = strdup("/");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering if (!rd)
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return log_oom();
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering unit = strdup("-.slice");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering if (!unit)
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return log_oom();
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering t = machine_new(m, MACHINE_HOST, ".host");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering if (!t)
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return log_oom();
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering t->leader = 1;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering t->id = mid;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering t->root_directory = rd;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering t->unit = unit;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering rd = unit = NULL;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering dual_timestamp_from_boottime_or_monotonic(&t->timestamp, 0);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering m->host_machine = t;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return 0;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering}
fbe550738d03b178bb004a1390e74115e904118aLennart 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
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering r = manager_add_host_machine(m);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering if (r < 0)
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return r;
fbe550738d03b178bb004a1390e74115e904118aLennart 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
e1427b138fbf7b7f13bb61187635b882be3ca2b2Michal Schmidt return log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
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
b9a8d250810d4803bc9bf6b36932b528cb991d1eLennart Poettering if (!machine_name_is_valid(de->d_name))
b9a8d250810d4803bc9bf6b36932b528cb991d1eLennart Poettering continue;
b9a8d250810d4803bc9bf6b36932b528cb991d1eLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering k = manager_add_machine(m, de->d_name, &machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (k < 0) {
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering r = log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
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) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_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',"
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering "member='PropertiesChanged',"
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering "arg0='org.freedesktop.systemd1.Unit'",
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
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering /* First, if we are not closing yet, initiate stopping */
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering if (!machine_check_gc(machine, drop_not_started) &&
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering machine_get_state(machine) != MACHINE_CLOSING)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine_stop(machine);
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering /* Now, the stop stop probably made this referenced
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering * again, but if it didn't, then it's time to let it
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering * go entirely. */
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering if (!machine_check_gc(machine, drop_not_started)) {
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering machine_finalize(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
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
0370612e0522191f929e3feb7d4937fff3d421e2Lennart 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:
3e044c492e3ebe64f4e3175c94f9db8a62557b82Markus Elfring manager_free(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}