machined.c revision 4afd3348c7506dd1d36305b7bcb9feb8952b9d6b
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/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m->machines = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m->machine_units = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!m->machines || !m->machine_units || !m->machine_leaders) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((machine = hashmap_first(m->machines)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((i = hashmap_steal_first(m->image_cache)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_event_source_unref(m->image_cache_defer_event);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus_verify_polkit_async_registry_free(m->polkit_registry);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int manager_add_host_machine(Manager *m) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_free_ char *rd = NULL, *unit = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to get machine ID: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering t = machine_new(m, MACHINE_HOST, ".host");
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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 */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Ignore symlinks that map the unit name to the machine */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering k = manager_add_machine(m, de->d_name, &machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to connect to system bus: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add manager object vtable: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable, machine_object_find, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add machine object vtable: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add machine enumerator: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable, image_object_find, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add image object vtable: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add image enumerator: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "type='signal',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "sender='org.freedesktop.systemd1',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "member='JobRemoved',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add match for JobRemoved: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "type='signal',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "sender='org.freedesktop.systemd1',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "member='UnitRemoved',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add match for UnitRemoved: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "type='signal',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "sender='org.freedesktop.systemd1',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "interface='org.freedesktop.DBus.Properties',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "member='PropertiesChanged',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "arg0='org.freedesktop.systemd1.Unit'",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "type='signal',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "sender='org.freedesktop.systemd1',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "interface='org.freedesktop.systemd1.Manager',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "member='Reloading',"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to add match for Reloading: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.systemd1.Manager",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to register name: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_attach_event(m->bus, m->event, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering 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)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* First, if we are not closing yet, initiate stopping */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!machine_check_gc(machine, drop_not_started) &&
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering machine_get_state(machine) != MACHINE_CLOSING)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Now, the stop stop probably made this referenced
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * again, but if it didn't, then it's time to let it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * go entirely. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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 */
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * existence of /run/systemd/machines/ to determine whether
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * machined is available, so please always make sure this
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * check stays in. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering mkdir_label("/run/systemd/machines", 0755);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(r, "Failed to fully start up daemon: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("systemd-machined running as pid "PID_FMT, getpid());
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "STATUS=Processing requests...");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("systemd-machined stopped as pid "PID_FMT, getpid());