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);
fbe550738d03b178bb004a1390e74115e904118aLennart Poetteringstatic int manager_add_host_machine(Manager *m) {
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering _cleanup_free_ char *rd = NULL, *unit = NULL;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering return log_error_errno(r, "Failed to get machine ID: %m");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering t = machine_new(m, MACHINE_HOST, ".host");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering dual_timestamp_from_boottime_or_monotonic(&t->timestamp, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint manager_enumerate_machines(Manager *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Read in machine data stored on disk */
e1427b138fbf7b7f13bb61187635b882be3ca2b2Michal Schmidt return 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);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering r = log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int manager_connect_bus(Manager *m) {
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_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',"
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering "member='PropertiesChanged',"
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering "arg0='org.freedesktop.systemd1.Unit'",
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);
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)
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)) {
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);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
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());