2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2013-07-02 03:47:23 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
#include "sd-daemon.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-10-30 02:06:55 +01:00
|
|
|
#include "bus-error.h"
|
2020-06-28 15:59:37 +02:00
|
|
|
#include "bus-locator.h"
|
2020-04-21 10:34:05 +02:00
|
|
|
#include "bus-log-control-api.h"
|
2020-01-22 11:39:22 +01:00
|
|
|
#include "bus-polkit.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "cgroup-util.h"
|
2015-10-26 20:07:55 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-08-23 14:30:52 +02:00
|
|
|
#include "hostname-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "label.h"
|
2014-12-28 02:44:37 +01:00
|
|
|
#include "machine-image.h"
|
2020-07-07 11:59:10 +02:00
|
|
|
#include "machined-varlink.h"
|
2014-12-19 20:43:18 +01:00
|
|
|
#include "machined.h"
|
2018-11-19 20:26:37 +01:00
|
|
|
#include "main-func.h"
|
2017-07-20 16:19:18 +02:00
|
|
|
#include "process-util.h"
|
2020-04-23 10:19:46 +02:00
|
|
|
#include "service-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "signal-util.h"
|
2017-12-07 14:28:00 +01:00
|
|
|
#include "special.h"
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static Manager* manager_unref(Manager *m);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
|
|
|
|
|
2018-12-07 16:47:20 +01:00
|
|
|
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops, char, string_hash_func, string_compare_func, Machine, machine_free);
|
2018-11-28 14:55:13 +01:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static int manager_new(Manager **ret) {
|
|
|
|
_cleanup_(manager_unrefp) Manager *m = NULL;
|
2013-10-30 02:06:55 +01:00
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
assert(ret);
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
m = new0(Manager, 1);
|
|
|
|
if (!m)
|
2018-04-05 12:38:25 +02:00
|
|
|
return -ENOMEM;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-12-07 16:47:20 +01:00
|
|
|
m->machines = hashmap_new(&machine_hash_ops);
|
2014-08-13 01:00:18 +02:00
|
|
|
m->machine_units = hashmap_new(&string_hash_ops);
|
2018-12-07 16:47:20 +01:00
|
|
|
m->machine_leaders = hashmap_new(NULL);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
if (!m->machines || !m->machine_units || !m->machine_leaders)
|
|
|
|
return -ENOMEM;
|
2013-10-30 02:06:55 +01:00
|
|
|
|
2013-11-11 19:34:13 +01:00
|
|
|
r = sd_event_default(&m->event);
|
2018-04-05 12:38:25 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
(void) sd_event_set_watchdog(m->event, true);
|
2013-12-11 18:14:52 +01:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
*ret = TAKE_PTR(m);
|
|
|
|
return 0;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static Manager* manager_unref(Manager *m) {
|
2018-04-24 18:16:44 +02:00
|
|
|
if (!m)
|
|
|
|
return NULL;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2016-04-29 19:14:52 +02:00
|
|
|
while (m->operations)
|
|
|
|
operation_free(m->operations);
|
|
|
|
|
|
|
|
assert(m->n_operations == 0);
|
|
|
|
|
2018-12-07 16:47:20 +01:00
|
|
|
hashmap_free(m->machines); /* This will free all machines, so that the machine_units/machine_leaders is empty */
|
2013-07-02 03:47:23 +02:00
|
|
|
hashmap_free(m->machine_units);
|
2013-11-06 02:03:04 +01:00
|
|
|
hashmap_free(m->machine_leaders);
|
2018-11-28 14:54:44 +01:00
|
|
|
hashmap_free(m->image_cache);
|
2014-12-28 02:44:37 +01:00
|
|
|
|
|
|
|
sd_event_source_unref(m->image_cache_defer_event);
|
2020-12-07 08:45:15 +01:00
|
|
|
#if ENABLE_NSCD
|
2018-12-07 16:49:52 +01:00
|
|
|
sd_event_source_unref(m->nscd_cache_flush_event);
|
2020-12-07 08:45:15 +01:00
|
|
|
#endif
|
2014-12-28 02:44:37 +01:00
|
|
|
|
2014-12-23 21:28:22 +01:00
|
|
|
bus_verify_polkit_async_registry_free(m->polkit_registry);
|
|
|
|
|
2020-07-07 11:59:10 +02:00
|
|
|
manager_varlink_done(m);
|
|
|
|
|
2019-01-17 15:53:15 +01:00
|
|
|
sd_bus_flush_close_unref(m->bus);
|
2013-10-30 02:06:55 +01:00
|
|
|
sd_event_unref(m->event);
|
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
return mfree(m);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
|
|
|
|
2015-08-24 21:05:09 +02:00
|
|
|
static int manager_add_host_machine(Manager *m) {
|
|
|
|
_cleanup_free_ char *rd = NULL, *unit = NULL;
|
|
|
|
sd_id128_t mid;
|
|
|
|
Machine *t;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (m->host_machine)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = sd_id128_get_machine(&mid);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get machine ID: %m");
|
|
|
|
|
|
|
|
rd = strdup("/");
|
|
|
|
if (!rd)
|
|
|
|
return log_oom();
|
|
|
|
|
2017-12-07 14:28:00 +01:00
|
|
|
unit = strdup(SPECIAL_ROOT_SLICE);
|
2015-08-24 21:05:09 +02:00
|
|
|
if (!unit)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
t = machine_new(m, MACHINE_HOST, ".host");
|
|
|
|
if (!t)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
t->leader = 1;
|
|
|
|
t->id = mid;
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
t->root_directory = TAKE_PTR(rd);
|
|
|
|
t->unit = TAKE_PTR(unit);
|
2015-08-24 21:05:09 +02:00
|
|
|
|
|
|
|
dual_timestamp_from_boottime_or_monotonic(&t->timestamp, 0);
|
|
|
|
|
|
|
|
m->host_machine = t;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static int manager_enumerate_machines(Manager *m) {
|
2013-07-02 03:47:23 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
|
|
|
struct dirent *de;
|
2020-08-29 23:21:20 +02:00
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2015-08-24 21:05:09 +02:00
|
|
|
r = manager_add_host_machine(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
/* Read in machine data stored on disk */
|
|
|
|
d = opendir("/run/systemd/machines");
|
|
|
|
if (!d) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-05 13:44:20 +01:00
|
|
|
return log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
|
|
|
struct Machine *machine;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (!dirent_is_file(de))
|
|
|
|
continue;
|
|
|
|
|
2014-02-11 21:06:51 +01:00
|
|
|
/* Ignore symlinks that map the unit name to the machine */
|
|
|
|
if (startswith(de->d_name, "unit:"))
|
|
|
|
continue;
|
|
|
|
|
2020-12-11 16:40:45 +01:00
|
|
|
if (!hostname_is_valid(de->d_name, 0))
|
2015-08-23 14:33:50 +02:00
|
|
|
continue;
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
k = manager_add_machine(m, de->d_name, &machine);
|
|
|
|
if (k < 0) {
|
2015-08-24 21:05:09 +02:00
|
|
|
r = log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
|
2013-07-02 03:47:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
machine_add_to_gc_queue(machine);
|
|
|
|
|
|
|
|
k = machine_load(machine);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int manager_connect_bus(Manager *m) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(!m->bus);
|
|
|
|
|
2013-11-11 22:00:48 +01:00
|
|
|
r = sd_bus_default_system(&m->bus);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to connect to system bus: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2020-04-23 14:21:46 +02:00
|
|
|
r = bus_add_implementation(m->bus, &manager_object, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2020-04-23 14:21:46 +02:00
|
|
|
return r;
|
2014-12-19 20:43:18 +01:00
|
|
|
|
2020-05-03 08:04:27 +02:00
|
|
|
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "JobRemoved", match_job_removed, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add match for JobRemoved: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2020-05-03 08:04:27 +02:00
|
|
|
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "UnitRemoved", match_unit_removed, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for UnitRemoved: %m");
|
|
|
|
|
|
|
|
r = sd_bus_match_signal_async(
|
|
|
|
m->bus,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
"PropertiesChanged",
|
|
|
|
match_properties_changed, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for PropertiesChanged: %m");
|
|
|
|
|
2020-05-03 08:04:27 +02:00
|
|
|
r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "Reloading", match_reloading, NULL, m);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-19 12:29:04 +01:00
|
|
|
return log_error_errno(r, "Failed to request match for Reloading: %m");
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2020-05-03 08:04:27 +02:00
|
|
|
r = bus_call_method_async(m->bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL);
|
2017-12-20 16:40:47 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enable subscription: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2020-04-21 10:34:05 +02:00
|
|
|
r = bus_log_control_api_register(m->bus);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2017-12-18 22:10:11 +01:00
|
|
|
r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.machine1", 0, NULL, NULL);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
2017-12-18 22:10:11 +01:00
|
|
|
return log_error_errno(r, "Failed to request name: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_attach_event(m->bus, m->event, 0);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to attach bus to event loop: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static void manager_gc(Manager *m, bool drop_not_started) {
|
2013-07-02 03:47:23 +02:00
|
|
|
Machine *machine;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
while ((machine = m->machine_gc_queue)) {
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
|
2013-07-02 03:47:23 +02:00
|
|
|
machine->in_gc_queue = false;
|
|
|
|
|
2015-08-06 15:50:54 +02:00
|
|
|
/* First, if we are not closing yet, initiate stopping */
|
2018-02-15 13:15:45 +01:00
|
|
|
if (machine_may_gc(machine, drop_not_started) &&
|
2015-08-06 15:50:54 +02:00
|
|
|
machine_get_state(machine) != MACHINE_CLOSING)
|
2013-07-02 03:47:23 +02:00
|
|
|
machine_stop(machine);
|
2015-08-06 15:50:54 +02:00
|
|
|
|
2016-07-10 14:48:23 +02:00
|
|
|
/* Now, the stop probably made this referenced
|
2015-08-06 15:50:54 +02:00
|
|
|
* again, but if it didn't, then it's time to let it
|
|
|
|
* go entirely. */
|
2018-02-15 13:15:45 +01:00
|
|
|
if (machine_may_gc(machine, drop_not_started)) {
|
2015-08-06 15:50:54 +02:00
|
|
|
machine_finalize(machine);
|
2013-07-02 03:47:23 +02:00
|
|
|
machine_free(machine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static int manager_startup(Manager *m) {
|
2013-07-02 03:47:23 +02:00
|
|
|
Machine *machine;
|
2013-10-30 02:06:55 +01:00
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
/* Connect to the bus */
|
|
|
|
r = manager_connect_bus(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2020-07-07 11:59:10 +02:00
|
|
|
/* Set up Varlink service */
|
|
|
|
r = manager_varlink_init(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
/* Deserialize state */
|
|
|
|
manager_enumerate_machines(m);
|
|
|
|
|
|
|
|
/* Remove stale objects before we start them */
|
|
|
|
manager_gc(m, false);
|
|
|
|
|
|
|
|
/* And start everything */
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(machine, m->machines)
|
2013-10-30 02:06:55 +01:00
|
|
|
machine_start(machine, NULL, NULL);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-19 21:15:08 +01:00
|
|
|
static bool check_idle(void *userdata) {
|
|
|
|
Manager *m = userdata;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2016-04-29 19:14:52 +02:00
|
|
|
if (m->operations)
|
|
|
|
return false;
|
|
|
|
|
2020-07-07 11:59:10 +02:00
|
|
|
if (varlink_server_current_connections(m->varlink_server) > 0)
|
|
|
|
return false;
|
|
|
|
|
2013-12-19 21:15:08 +01:00
|
|
|
manager_gc(m, true);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-12-19 21:15:08 +01:00
|
|
|
return hashmap_isempty(m->machines);
|
|
|
|
}
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
static int manager_run(Manager *m) {
|
2013-12-19 21:15:08 +01:00
|
|
|
assert(m);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-12-19 21:15:08 +01:00
|
|
|
return bus_event_loop_with_idle(
|
|
|
|
m->event,
|
|
|
|
m->bus,
|
|
|
|
"org.freedesktop.machine1",
|
|
|
|
DEFAULT_EXIT_USEC,
|
|
|
|
check_idle, m);
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 14:44:14 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-04-05 12:38:25 +02:00
|
|
|
_cleanup_(manager_unrefp) Manager *m = NULL;
|
2013-07-02 03:47:23 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
log_set_facility(LOG_AUTH);
|
2018-11-20 11:18:22 +01:00
|
|
|
log_setup_service();
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2020-04-23 10:19:46 +02:00
|
|
|
r = service_parse_argv("systemd-machined.service",
|
|
|
|
"Manage registrations of local VMs and containers.",
|
2020-04-23 14:21:46 +02:00
|
|
|
BUS_IMPLEMENTATIONS(&manager_object,
|
|
|
|
&log_control_object),
|
2020-04-23 10:19:46 +02:00
|
|
|
argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2020-04-23 10:19:46 +02:00
|
|
|
umask(0022);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
/* 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. */
|
|
|
|
(void) mkdir_label("/run/systemd/machines", 0755);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGTERM, SIGINT, -1) >= 0);
|
2015-02-17 19:22:03 +01:00
|
|
|
|
2018-04-05 12:38:25 +02:00
|
|
|
r = manager_new(&m);
|
2018-11-16 14:44:14 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to allocate manager object: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
r = manager_startup(m);
|
2018-11-16 14:44:14 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to fully start up daemon: %m");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
log_debug("systemd-machined running as pid "PID_FMT, getpid_cached());
|
2018-04-24 18:16:44 +02:00
|
|
|
(void) sd_notify(false,
|
|
|
|
"READY=1\n"
|
|
|
|
"STATUS=Processing requests...");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
r = manager_run(m);
|
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
log_debug("systemd-machined stopped as pid "PID_FMT, getpid_cached());
|
2018-04-24 18:16:44 +02:00
|
|
|
(void) sd_notify(false,
|
|
|
|
"STOPPING=1\n"
|
|
|
|
"STATUS=Shutting down...");
|
|
|
|
|
2018-11-16 14:44:14 +01:00
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
2018-11-16 14:44:14 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|