]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
12 #include "sd-daemon.h"
14 #include "alloc-util.h"
15 #include "bus-error.h"
17 #include "cgroup-util.h"
18 #include "dirent-util.h"
20 #include "format-util.h"
21 #include "hostname-util.h"
23 #include "machine-image.h"
25 #include "process-util.h"
26 #include "signal-util.h"
29 static Manager
* manager_unref(Manager
*m
);
30 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
32 static int manager_new(Manager
**ret
) {
33 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
42 m
->machines
= hashmap_new(&string_hash_ops
);
43 m
->machine_units
= hashmap_new(&string_hash_ops
);
44 m
->machine_leaders
= hashmap_new(NULL
);
46 if (!m
->machines
|| !m
->machine_units
|| !m
->machine_leaders
)
49 r
= sd_event_default(&m
->event
);
53 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
57 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
61 (void) sd_event_set_watchdog(m
->event
, true);
67 static Manager
* manager_unref(Manager
*m
) {
73 operation_free(m
->operations
);
75 assert(m
->n_operations
== 0);
77 while ((machine
= hashmap_first(m
->machines
)))
78 machine_free(machine
);
80 hashmap_free(m
->machines
);
81 hashmap_free(m
->machine_units
);
82 hashmap_free(m
->machine_leaders
);
84 hashmap_free_with_destructor(m
->image_cache
, image_unref
);
86 sd_event_source_unref(m
->image_cache_defer_event
);
88 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
91 sd_event_unref(m
->event
);
96 static int manager_add_host_machine(Manager
*m
) {
97 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
105 r
= sd_id128_get_machine(&mid
);
107 return log_error_errno(r
, "Failed to get machine ID: %m");
113 unit
= strdup(SPECIAL_ROOT_SLICE
);
117 t
= machine_new(m
, MACHINE_HOST
, ".host");
124 t
->root_directory
= TAKE_PTR(rd
);
125 t
->unit
= TAKE_PTR(unit
);
127 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
134 static int manager_enumerate_machines(Manager
*m
) {
135 _cleanup_closedir_
DIR *d
= NULL
;
141 r
= manager_add_host_machine(m
);
145 /* Read in machine data stored on disk */
146 d
= opendir("/run/systemd/machines");
151 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
154 FOREACH_DIRENT(de
, d
, return -errno
) {
155 struct Machine
*machine
;
158 if (!dirent_is_file(de
))
161 /* Ignore symlinks that map the unit name to the machine */
162 if (startswith(de
->d_name
, "unit:"))
165 if (!machine_name_is_valid(de
->d_name
))
168 k
= manager_add_machine(m
, de
->d_name
, &machine
);
170 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
174 machine_add_to_gc_queue(machine
);
176 k
= machine_load(machine
);
184 static int manager_connect_bus(Manager
*m
) {
190 r
= sd_bus_default_system(&m
->bus
);
192 return log_error_errno(r
, "Failed to connect to system bus: %m");
194 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
196 return log_error_errno(r
, "Failed to add manager object vtable: %m");
198 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
200 return log_error_errno(r
, "Failed to add machine object vtable: %m");
202 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
204 return log_error_errno(r
, "Failed to add machine enumerator: %m");
206 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
208 return log_error_errno(r
, "Failed to add image object vtable: %m");
210 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
212 return log_error_errno(r
, "Failed to add image enumerator: %m");
214 r
= sd_bus_match_signal_async(
217 "org.freedesktop.systemd1",
218 "/org/freedesktop/systemd1",
219 "org.freedesktop.systemd1.Manager",
221 match_job_removed
, NULL
, m
);
223 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
225 r
= sd_bus_match_signal_async(
228 "org.freedesktop.systemd1",
229 "/org/freedesktop/systemd1",
230 "org.freedesktop.systemd1.Manager",
232 match_unit_removed
, NULL
, m
);
234 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
236 r
= sd_bus_match_signal_async(
239 "org.freedesktop.systemd1",
241 "org.freedesktop.DBus.Properties",
243 match_properties_changed
, NULL
, m
);
245 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
247 r
= sd_bus_match_signal_async(
250 "org.freedesktop.systemd1",
251 "/org/freedesktop/systemd1",
252 "org.freedesktop.systemd1.Manager",
254 match_reloading
, NULL
, m
);
256 return log_error_errno(r
, "Failed to request match for Reloading: %m");
258 r
= sd_bus_call_method_async(
261 "org.freedesktop.systemd1",
262 "/org/freedesktop/systemd1",
263 "org.freedesktop.systemd1.Manager",
268 return log_error_errno(r
, "Failed to enable subscription: %m");
270 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
272 return log_error_errno(r
, "Failed to request name: %m");
274 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
276 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
281 static void manager_gc(Manager
*m
, bool drop_not_started
) {
286 while ((machine
= m
->machine_gc_queue
)) {
287 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
288 machine
->in_gc_queue
= false;
290 /* First, if we are not closing yet, initiate stopping */
291 if (machine_may_gc(machine
, drop_not_started
) &&
292 machine_get_state(machine
) != MACHINE_CLOSING
)
293 machine_stop(machine
);
295 /* Now, the stop probably made this referenced
296 * again, but if it didn't, then it's time to let it
298 if (machine_may_gc(machine
, drop_not_started
)) {
299 machine_finalize(machine
);
300 machine_free(machine
);
305 static int manager_startup(Manager
*m
) {
312 /* Connect to the bus */
313 r
= manager_connect_bus(m
);
317 /* Deserialize state */
318 manager_enumerate_machines(m
);
320 /* Remove stale objects before we start them */
321 manager_gc(m
, false);
323 /* And start everything */
324 HASHMAP_FOREACH(machine
, m
->machines
, i
)
325 machine_start(machine
, NULL
, NULL
);
330 static bool check_idle(void *userdata
) {
331 Manager
*m
= userdata
;
338 return hashmap_isempty(m
->machines
);
341 static int manager_run(Manager
*m
) {
344 return bus_event_loop_with_idle(
347 "org.freedesktop.machine1",
352 int main(int argc
, char *argv
[]) {
353 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
356 log_set_target(LOG_TARGET_AUTO
);
357 log_set_facility(LOG_AUTH
);
358 log_parse_environment();
364 log_error("This program takes no arguments.");
369 /* Always create the directories people can create inotify watches in. Note that some applications might check
370 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
371 * make sure this check stays in. */
372 (void) mkdir_label("/run/systemd/machines", 0755);
374 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
378 log_error_errno(r
, "Failed to allocate manager object: %m");
382 r
= manager_startup(m
);
384 log_error_errno(r
, "Failed to fully start up daemon: %m");
388 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
392 "STATUS=Processing requests...");
396 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
399 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;