machined.c revision 5bb658a1784a0fd4f0f32adb4b1fb636ff503f7d
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <pwd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <fcntl.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <string.h>
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#include <unistd.h>
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include <sys/epoll.h>
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering#include "sd-daemon.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "strv.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "conf-parser.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "cgroup-util.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "mkdir.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "bus-util.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "bus-error.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "machined.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart PoetteringManager *manager_new(void) {
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack Manager *m;
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack int r;
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering m = new0(Manager, 1);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (!m)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return NULL;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering m->machines = hashmap_new(string_hash_func, string_compare_func);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering m->machine_units = hashmap_new(string_hash_func, string_compare_func);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering m->machine_leaders = hashmap_new(trivial_hash_func, trivial_compare_func);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (!m->machines || !m->machine_units || !m->machine_leaders) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering manager_free(m);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return NULL;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering }
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering r = sd_event_default(&m->event);
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering if (r < 0) {
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering manager_free(m);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return NULL;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering }
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering sd_event_set_watchdog(m->event, true);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return m;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering}
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringvoid manager_free(Manager *m) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering Machine *machine;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering assert(m);
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering while ((machine = hashmap_first(m->machines)))
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering machine_free(machine);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering hashmap_free(m->machines);
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering hashmap_free(m->machine_units);
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering hashmap_free(m->machine_leaders);
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering sd_bus_unref(m->bus);
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering sd_event_unref(m->event);
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint manager_enumerate_machines(Manager *m) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct dirent *de;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering int r = 0;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering assert(m);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering /* Read in machine data stored on disk */
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering d = opendir("/run/systemd/machines");
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (!d) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (errno == ENOENT)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return 0;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering log_error("Failed to open /run/systemd/machines: %m");
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return -errno;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering FOREACH_DIRENT(de, d, return -errno) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering struct Machine *machine;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int k;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!dirent_is_file(de))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k = manager_add_machine(m, de->d_name, &machine);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (k < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to add machine by file name %s: %s", de->d_name, strerror(-k));
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering r = k;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering continue;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering }
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering machine_add_to_gc_queue(machine);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = machine_load(machine);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (k < 0)
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering r = k;
d75acfb059ece4512278b8820a9103664996f1e5Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringstatic int manager_connect_bus(Manager *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(!m->bus);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_default_system(&m->bus);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to connect to system bus: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek r = sd_bus_add_object_vtable(m->bus, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0) {
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poettering log_error("Failed to add manager object vtable: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_add_fallback_vtable(m->bus, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable, machine_object_find, m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to add machine object vtable: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = sd_bus_add_node_enumerator(m->bus, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_error("Failed to add machine enumerator: %s", strerror(-r));
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return r;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = sd_bus_add_match(m->bus,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "type='signal',"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering "sender='org.freedesktop.systemd1',"
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek "interface='org.freedesktop.systemd1.Manager',"
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek "member='JobRemoved',"
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek "path='/org/freedesktop/systemd1'",
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek match_job_removed,
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek m);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0) {
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error("Failed to add match for JobRemoved: %s", strerror(-r));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = sd_bus_add_match(m->bus,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "type='signal',"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "sender='org.freedesktop.systemd1',"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "interface='org.freedesktop.systemd1.Manager',"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "member='UnitRemoved',"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "path='/org/freedesktop/systemd1'",
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering match_unit_removed,
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen m);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (r < 0) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_error("Failed to add match for UnitRemoved: %s", strerror(-r));
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return r;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen }
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = sd_bus_add_match(m->bus,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek "type='signal',"
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek "sender='org.freedesktop.systemd1',"
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek "interface='org.freedesktop.DBus.Properties',"
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering "member='PropertiesChanged'",
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering match_properties_changed,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek m);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_error("Failed to add match for PropertiesChanged: %s", strerror(-r));
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = sd_bus_add_match(m->bus,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "type='signal',"
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "sender='org.freedesktop.systemd1',"
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "interface='org.freedesktop.systemd1.Manager',"
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "member='Reloading',"
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "path='/org/freedesktop/systemd1'",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek match_reloading,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek m);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error("Failed to add match for Reloading: %s", strerror(-r));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
9ead3519c54b6d1b79b35541873b5cf7c8b3a7d3Lennart Poettering r = sd_bus_call_method(
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen m->bus,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "org.freedesktop.systemd1",
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "/org/freedesktop/systemd1",
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "org.freedesktop.systemd1.Manager",
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen "Subscribe",
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering &error,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering NULL, NULL);
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering if (r < 0) {
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering return r;
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering }
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering r = sd_bus_request_name(m->bus, "org.freedesktop.machine1", 0);
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering if (r < 0) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_error("Failed to register name: %s", strerror(-r));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = sd_bus_attach_event(m->bus, m->event, 0);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_error("Failed to attach bus to event loop: %s", strerror(-r));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringvoid manager_gc(Manager *m, bool drop_not_started) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Machine *machine;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering while ((machine = m->machine_gc_queue)) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering machine->in_gc_queue = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!machine_check_gc(machine, drop_not_started)) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering machine_stop(machine);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen machine_free(machine);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poetteringint manager_startup(Manager *m) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Machine *machine;
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering Iterator i;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen int r;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* Connect to the bus */
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack r = manager_connect_bus(m);
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack if (r < 0)
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack return r;
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Deserialize state */
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering manager_enumerate_machines(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Remove stale objects before we start them */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering manager_gc(m, false);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* And start everything */
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering HASHMAP_FOREACH(machine, m->machines, i)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering machine_start(machine, NULL, NULL);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return 0;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint manager_run(Manager *m) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int r;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering assert(m);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering for (;;) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_event_get_state(m->event);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (r < 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
if (r == SD_EVENT_FINISHED)
return 0;
manager_gc(m, true);
r = sd_event_run(m->event, (uint64_t) -1);
if (r < 0)
return r;
}
return 0;
}
int main(int argc, char *argv[]) {
Manager *m = NULL;
int r;
log_set_target(LOG_TARGET_AUTO);
log_set_facility(LOG_AUTH);
log_parse_environment();
log_open();
umask(0022);
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
/* Always create the directories people can create inotify
* watches in. Note that some applications might check for the
* existence of /run/systemd/machines/ to determine whether
* machined is available, so please always make sure this
* check stays in. */
mkdir_label("/run/systemd/machines", 0755);
m = manager_new();
if (!m) {
r = log_oom();
goto finish;
}
r = manager_startup(m);
if (r < 0) {
log_error("Failed to fully start up daemon: %s", strerror(-r));
goto finish;
}
log_debug("systemd-machined running as pid %lu", (unsigned long) getpid());
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
r = manager_run(m);
log_debug("systemd-machined stopped as pid %lu", (unsigned long) getpid());
finish:
sd_notify(false,
"STATUS=Shutting down...");
if (m)
manager_free(m);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}