]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "bus-error.h"
12 #include "cgroup-util.h"
13 #include "dirent-util.h"
15 #include "format-util.h"
16 #include "hostname-util.h"
18 #include "machine-image.h"
20 #include "main-func.h"
21 #include "process-util.h"
22 #include "signal-util.h"
25 static Manager
* manager_unref(Manager
*m
);
26 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
28 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops
, char, string_hash_func
, string_compare_func
, Machine
, machine_free
);
30 static int manager_new(Manager
**ret
) {
31 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
40 m
->machines
= hashmap_new(&machine_hash_ops
);
41 m
->machine_units
= hashmap_new(&string_hash_ops
);
42 m
->machine_leaders
= hashmap_new(NULL
);
44 if (!m
->machines
|| !m
->machine_units
|| !m
->machine_leaders
)
47 r
= sd_event_default(&m
->event
);
51 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
55 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
59 (void) sd_event_set_watchdog(m
->event
, true);
65 static Manager
* manager_unref(Manager
*m
) {
70 operation_free(m
->operations
);
72 assert(m
->n_operations
== 0);
74 hashmap_free(m
->machines
); /* This will free all machines, so that the machine_units/machine_leaders is empty */
75 hashmap_free(m
->machine_units
);
76 hashmap_free(m
->machine_leaders
);
77 hashmap_free(m
->image_cache
);
79 sd_event_source_unref(m
->image_cache_defer_event
);
80 sd_event_source_unref(m
->nscd_cache_flush_event
);
82 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
85 sd_event_unref(m
->event
);
90 static int manager_add_host_machine(Manager
*m
) {
91 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
99 r
= sd_id128_get_machine(&mid
);
101 return log_error_errno(r
, "Failed to get machine ID: %m");
107 unit
= strdup(SPECIAL_ROOT_SLICE
);
111 t
= machine_new(m
, MACHINE_HOST
, ".host");
118 t
->root_directory
= TAKE_PTR(rd
);
119 t
->unit
= TAKE_PTR(unit
);
121 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
128 static int manager_enumerate_machines(Manager
*m
) {
129 _cleanup_closedir_
DIR *d
= NULL
;
135 r
= manager_add_host_machine(m
);
139 /* Read in machine data stored on disk */
140 d
= opendir("/run/systemd/machines");
145 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
148 FOREACH_DIRENT(de
, d
, return -errno
) {
149 struct Machine
*machine
;
152 if (!dirent_is_file(de
))
155 /* Ignore symlinks that map the unit name to the machine */
156 if (startswith(de
->d_name
, "unit:"))
159 if (!machine_name_is_valid(de
->d_name
))
162 k
= manager_add_machine(m
, de
->d_name
, &machine
);
164 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
168 machine_add_to_gc_queue(machine
);
170 k
= machine_load(machine
);
178 static int manager_connect_bus(Manager
*m
) {
184 r
= sd_bus_default_system(&m
->bus
);
186 return log_error_errno(r
, "Failed to connect to system bus: %m");
188 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
190 return log_error_errno(r
, "Failed to add manager object vtable: %m");
192 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
194 return log_error_errno(r
, "Failed to add machine object vtable: %m");
196 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
198 return log_error_errno(r
, "Failed to add machine enumerator: %m");
200 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
202 return log_error_errno(r
, "Failed to add image object vtable: %m");
204 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
206 return log_error_errno(r
, "Failed to add image enumerator: %m");
208 r
= sd_bus_match_signal_async(
211 "org.freedesktop.systemd1",
212 "/org/freedesktop/systemd1",
213 "org.freedesktop.systemd1.Manager",
215 match_job_removed
, NULL
, m
);
217 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
219 r
= sd_bus_match_signal_async(
222 "org.freedesktop.systemd1",
223 "/org/freedesktop/systemd1",
224 "org.freedesktop.systemd1.Manager",
226 match_unit_removed
, NULL
, m
);
228 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
230 r
= sd_bus_match_signal_async(
233 "org.freedesktop.systemd1",
235 "org.freedesktop.DBus.Properties",
237 match_properties_changed
, NULL
, m
);
239 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
241 r
= sd_bus_match_signal_async(
244 "org.freedesktop.systemd1",
245 "/org/freedesktop/systemd1",
246 "org.freedesktop.systemd1.Manager",
248 match_reloading
, NULL
, m
);
250 return log_error_errno(r
, "Failed to request match for Reloading: %m");
252 r
= sd_bus_call_method_async(
255 "org.freedesktop.systemd1",
256 "/org/freedesktop/systemd1",
257 "org.freedesktop.systemd1.Manager",
262 return log_error_errno(r
, "Failed to enable subscription: %m");
264 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
266 return log_error_errno(r
, "Failed to request name: %m");
268 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
270 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
275 static void manager_gc(Manager
*m
, bool drop_not_started
) {
280 while ((machine
= m
->machine_gc_queue
)) {
281 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
282 machine
->in_gc_queue
= false;
284 /* First, if we are not closing yet, initiate stopping */
285 if (machine_may_gc(machine
, drop_not_started
) &&
286 machine_get_state(machine
) != MACHINE_CLOSING
)
287 machine_stop(machine
);
289 /* Now, the stop probably made this referenced
290 * again, but if it didn't, then it's time to let it
292 if (machine_may_gc(machine
, drop_not_started
)) {
293 machine_finalize(machine
);
294 machine_free(machine
);
299 static int manager_startup(Manager
*m
) {
306 /* Connect to the bus */
307 r
= manager_connect_bus(m
);
311 /* Deserialize state */
312 manager_enumerate_machines(m
);
314 /* Remove stale objects before we start them */
315 manager_gc(m
, false);
317 /* And start everything */
318 HASHMAP_FOREACH(machine
, m
->machines
, i
)
319 machine_start(machine
, NULL
, NULL
);
324 static bool check_idle(void *userdata
) {
325 Manager
*m
= userdata
;
332 return hashmap_isempty(m
->machines
);
335 static int manager_run(Manager
*m
) {
338 return bus_event_loop_with_idle(
341 "org.freedesktop.machine1",
346 static int run(int argc
, char *argv
[]) {
347 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
350 log_set_facility(LOG_AUTH
);
356 log_error("This program takes no arguments.");
360 /* Always create the directories people can create inotify watches in. Note that some applications might check
361 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
362 * make sure this check stays in. */
363 (void) mkdir_label("/run/systemd/machines", 0755);
365 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
369 return log_error_errno(r
, "Failed to allocate manager object: %m");
371 r
= manager_startup(m
);
373 return log_error_errno(r
, "Failed to fully start up daemon: %m");
375 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
376 (void) sd_notify(false,
378 "STATUS=Processing requests...");
382 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
383 (void) sd_notify(false,
385 "STATUS=Shutting down...");
390 DEFINE_MAIN_FUNCTION(run
);