machined.c revision 1ddb263d21099ae42195c2bc382bdf72a7f24f82
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart 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 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 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/>.
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt m->machine_units = hashmap_new(&string_hash_ops);
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering if (!m->machines || !m->machine_units || !m->machine_leaders) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((machine = hashmap_first(m->machines)))
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering while ((i = hashmap_steal_first(m->image_cache)))
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering sd_event_source_unref(m->image_cache_defer_event);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering bus_verify_polkit_async_registry_free(m->polkit_registry);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint manager_enumerate_machines(Manager *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Read in machine data stored on disk */
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering /* Ignore symlinks that map the unit name to the machine */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering k = manager_add_machine(m, de->d_name, &machine);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int manager_connect_bus(Manager *m) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to connect to system bus: %m");
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add manager object vtable: %m");
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 return log_error_errno(r, "Failed to add machine object vtable: %m");
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add machine enumerator: %m");
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 return log_error_errno(r, "Failed to add image object vtable: %m");
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering return log_error_errno(r, "Failed to add image enumerator: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='JobRemoved',"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for JobRemoved: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='UnitRemoved',"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for UnitRemoved: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.DBus.Properties',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='PropertiesChanged'",
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "type='signal',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "sender='org.freedesktop.systemd1',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering "member='Reloading',"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add match for Reloading: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.systemd1.Manager",
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
5bb658a1784a0fd4f0f32adb4b1fb636ff503f7dKay Sievers r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", 0);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to register name: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = sd_bus_attach_event(m->bus, m->event, 0);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to attach bus to event loop: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringvoid manager_gc(Manager *m, bool drop_not_started) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((machine = m->machine_gc_queue)) {
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (!machine_check_gc(machine, drop_not_started)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Connect to the bus */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Deserialize state */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Remove stale objects before we start them */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* And start everything */
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering "org.freedesktop.machine1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("This program takes no arguments.");
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);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to fully start up daemon: %m");
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("systemd-machined running as pid "PID_FMT, getpid());
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "STATUS=Processing requests...");
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("systemd-machined stopped as pid "PID_FMT, getpid());