]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined.c
2828ed61b0353e393c5bc6c8a617f3f6b8960c25
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-locator.h"
14 #include "bus-log-control-api.h"
15 #include "bus-polkit.h"
16 #include "cgroup-util.h"
17 #include "dirent-util.h"
19 #include "format-util.h"
20 #include "hostname-util.h"
22 #include "machine-image.h"
23 #include "machined-varlink.h"
25 #include "main-func.h"
26 #include "process-util.h"
27 #include "service-util.h"
28 #include "signal-util.h"
31 static Manager
* manager_unref(Manager
*m
);
32 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
34 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops
, char, string_hash_func
, string_compare_func
, Machine
, machine_free
);
36 static int manager_new(Manager
**ret
) {
37 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
46 m
->machines
= hashmap_new(&machine_hash_ops
);
47 m
->machine_units
= hashmap_new(&string_hash_ops
);
48 m
->machine_leaders
= hashmap_new(NULL
);
50 if (!m
->machines
|| !m
->machine_units
|| !m
->machine_leaders
)
53 r
= sd_event_default(&m
->event
);
57 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
61 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
65 (void) sd_event_set_watchdog(m
->event
, true);
71 static Manager
* manager_unref(Manager
*m
) {
76 operation_free(m
->operations
);
78 assert(m
->n_operations
== 0);
80 hashmap_free(m
->machines
); /* This will free all machines, so that the machine_units/machine_leaders is empty */
81 hashmap_free(m
->machine_units
);
82 hashmap_free(m
->machine_leaders
);
83 hashmap_free(m
->image_cache
);
85 sd_event_source_unref(m
->image_cache_defer_event
);
86 sd_event_source_unref(m
->nscd_cache_flush_event
);
88 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
90 manager_varlink_done(m
);
92 sd_bus_flush_close_unref(m
->bus
);
93 sd_event_unref(m
->event
);
98 static int manager_add_host_machine(Manager
*m
) {
99 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
107 r
= sd_id128_get_machine(&mid
);
109 return log_error_errno(r
, "Failed to get machine ID: %m");
115 unit
= strdup(SPECIAL_ROOT_SLICE
);
119 t
= machine_new(m
, MACHINE_HOST
, ".host");
126 t
->root_directory
= TAKE_PTR(rd
);
127 t
->unit
= TAKE_PTR(unit
);
129 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
136 static int manager_enumerate_machines(Manager
*m
) {
137 _cleanup_closedir_
DIR *d
= NULL
;
143 r
= manager_add_host_machine(m
);
147 /* Read in machine data stored on disk */
148 d
= opendir("/run/systemd/machines");
153 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
156 FOREACH_DIRENT(de
, d
, return -errno
) {
157 struct Machine
*machine
;
160 if (!dirent_is_file(de
))
163 /* Ignore symlinks that map the unit name to the machine */
164 if (startswith(de
->d_name
, "unit:"))
167 if (!machine_name_is_valid(de
->d_name
))
170 k
= manager_add_machine(m
, de
->d_name
, &machine
);
172 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
176 machine_add_to_gc_queue(machine
);
178 k
= machine_load(machine
);
186 static int manager_connect_bus(Manager
*m
) {
192 r
= sd_bus_default_system(&m
->bus
);
194 return log_error_errno(r
, "Failed to connect to system bus: %m");
196 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
200 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "JobRemoved", match_job_removed
, NULL
, m
);
202 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
204 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "UnitRemoved", match_unit_removed
, NULL
, m
);
206 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
208 r
= sd_bus_match_signal_async(
211 "org.freedesktop.systemd1",
213 "org.freedesktop.DBus.Properties",
215 match_properties_changed
, NULL
, m
);
217 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
219 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "Reloading", match_reloading
, NULL
, m
);
221 return log_error_errno(r
, "Failed to request match for Reloading: %m");
223 r
= bus_call_method_async(m
->bus
, NULL
, bus_systemd_mgr
, "Subscribe", NULL
, NULL
, NULL
);
225 return log_error_errno(r
, "Failed to enable subscription: %m");
227 r
= bus_log_control_api_register(m
->bus
);
231 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
233 return log_error_errno(r
, "Failed to request name: %m");
235 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
237 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
242 static void manager_gc(Manager
*m
, bool drop_not_started
) {
247 while ((machine
= m
->machine_gc_queue
)) {
248 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
249 machine
->in_gc_queue
= false;
251 /* First, if we are not closing yet, initiate stopping */
252 if (machine_may_gc(machine
, drop_not_started
) &&
253 machine_get_state(machine
) != MACHINE_CLOSING
)
254 machine_stop(machine
);
256 /* Now, the stop probably made this referenced
257 * again, but if it didn't, then it's time to let it
259 if (machine_may_gc(machine
, drop_not_started
)) {
260 machine_finalize(machine
);
261 machine_free(machine
);
266 static int manager_startup(Manager
*m
) {
272 /* Connect to the bus */
273 r
= manager_connect_bus(m
);
277 /* Set up Varlink service */
278 r
= manager_varlink_init(m
);
282 /* Deserialize state */
283 manager_enumerate_machines(m
);
285 /* Remove stale objects before we start them */
286 manager_gc(m
, false);
288 /* And start everything */
289 HASHMAP_FOREACH(machine
, m
->machines
)
290 machine_start(machine
, NULL
, NULL
);
295 static bool check_idle(void *userdata
) {
296 Manager
*m
= userdata
;
301 if (varlink_server_current_connections(m
->varlink_server
) > 0)
306 return hashmap_isempty(m
->machines
);
309 static int manager_run(Manager
*m
) {
312 return bus_event_loop_with_idle(
315 "org.freedesktop.machine1",
320 static int run(int argc
, char *argv
[]) {
321 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
324 log_set_facility(LOG_AUTH
);
327 r
= service_parse_argv("systemd-machined.service",
328 "Manage registrations of local VMs and containers.",
329 BUS_IMPLEMENTATIONS(&manager_object
,
330 &log_control_object
),
337 /* Always create the directories people can create inotify watches in. Note that some applications might check
338 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
339 * make sure this check stays in. */
340 (void) mkdir_label("/run/systemd/machines", 0755);
342 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
346 return log_error_errno(r
, "Failed to allocate manager object: %m");
348 r
= manager_startup(m
);
350 return log_error_errno(r
, "Failed to fully start up daemon: %m");
352 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
353 (void) sd_notify(false,
355 "STATUS=Processing requests...");
359 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
360 (void) sd_notify(false,
362 "STATUS=Shutting down...");
367 DEFINE_MAIN_FUNCTION(run
);