]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-polkit.h"
14 #include "cgroup-util.h"
15 #include "dirent-util.h"
17 #include "format-util.h"
18 #include "hostname-util.h"
20 #include "machine-image.h"
22 #include "main-func.h"
23 #include "process-util.h"
24 #include "signal-util.h"
27 static Manager
* manager_unref(Manager
*m
);
28 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
30 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops
, char, string_hash_func
, string_compare_func
, Machine
, machine_free
);
32 static int manager_new(Manager
**ret
) {
33 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
42 m
->machines
= hashmap_new(&machine_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
) {
72 operation_free(m
->operations
);
74 assert(m
->n_operations
== 0);
76 hashmap_free(m
->machines
); /* This will free all machines, so that the machine_units/machine_leaders is empty */
77 hashmap_free(m
->machine_units
);
78 hashmap_free(m
->machine_leaders
);
79 hashmap_free(m
->image_cache
);
81 sd_event_source_unref(m
->image_cache_defer_event
);
82 sd_event_source_unref(m
->nscd_cache_flush_event
);
84 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
86 sd_bus_flush_close_unref(m
->bus
);
87 sd_event_unref(m
->event
);
92 static int manager_add_host_machine(Manager
*m
) {
93 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
101 r
= sd_id128_get_machine(&mid
);
103 return log_error_errno(r
, "Failed to get machine ID: %m");
109 unit
= strdup(SPECIAL_ROOT_SLICE
);
113 t
= machine_new(m
, MACHINE_HOST
, ".host");
120 t
->root_directory
= TAKE_PTR(rd
);
121 t
->unit
= TAKE_PTR(unit
);
123 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
130 static int manager_enumerate_machines(Manager
*m
) {
131 _cleanup_closedir_
DIR *d
= NULL
;
137 r
= manager_add_host_machine(m
);
141 /* Read in machine data stored on disk */
142 d
= opendir("/run/systemd/machines");
147 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
150 FOREACH_DIRENT(de
, d
, return -errno
) {
151 struct Machine
*machine
;
154 if (!dirent_is_file(de
))
157 /* Ignore symlinks that map the unit name to the machine */
158 if (startswith(de
->d_name
, "unit:"))
161 if (!machine_name_is_valid(de
->d_name
))
164 k
= manager_add_machine(m
, de
->d_name
, &machine
);
166 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
170 machine_add_to_gc_queue(machine
);
172 k
= machine_load(machine
);
180 static int manager_connect_bus(Manager
*m
) {
186 r
= sd_bus_default_system(&m
->bus
);
188 return log_error_errno(r
, "Failed to connect to system bus: %m");
190 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
192 return log_error_errno(r
, "Failed to add manager object vtable: %m");
194 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
196 return log_error_errno(r
, "Failed to add machine object vtable: %m");
198 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
200 return log_error_errno(r
, "Failed to add machine enumerator: %m");
202 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
204 return log_error_errno(r
, "Failed to add image object vtable: %m");
206 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
208 return log_error_errno(r
, "Failed to add image enumerator: %m");
210 r
= sd_bus_match_signal_async(
213 "org.freedesktop.systemd1",
214 "/org/freedesktop/systemd1",
215 "org.freedesktop.systemd1.Manager",
217 match_job_removed
, NULL
, m
);
219 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
221 r
= sd_bus_match_signal_async(
224 "org.freedesktop.systemd1",
225 "/org/freedesktop/systemd1",
226 "org.freedesktop.systemd1.Manager",
228 match_unit_removed
, NULL
, m
);
230 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
232 r
= sd_bus_match_signal_async(
235 "org.freedesktop.systemd1",
237 "org.freedesktop.DBus.Properties",
239 match_properties_changed
, NULL
, m
);
241 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
243 r
= sd_bus_match_signal_async(
246 "org.freedesktop.systemd1",
247 "/org/freedesktop/systemd1",
248 "org.freedesktop.systemd1.Manager",
250 match_reloading
, NULL
, m
);
252 return log_error_errno(r
, "Failed to request match for Reloading: %m");
254 r
= sd_bus_call_method_async(
257 "org.freedesktop.systemd1",
258 "/org/freedesktop/systemd1",
259 "org.freedesktop.systemd1.Manager",
264 return log_error_errno(r
, "Failed to enable subscription: %m");
266 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
268 return log_error_errno(r
, "Failed to request name: %m");
270 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
272 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
277 static void manager_gc(Manager
*m
, bool drop_not_started
) {
282 while ((machine
= m
->machine_gc_queue
)) {
283 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
284 machine
->in_gc_queue
= false;
286 /* First, if we are not closing yet, initiate stopping */
287 if (machine_may_gc(machine
, drop_not_started
) &&
288 machine_get_state(machine
) != MACHINE_CLOSING
)
289 machine_stop(machine
);
291 /* Now, the stop probably made this referenced
292 * again, but if it didn't, then it's time to let it
294 if (machine_may_gc(machine
, drop_not_started
)) {
295 machine_finalize(machine
);
296 machine_free(machine
);
301 static int manager_startup(Manager
*m
) {
308 /* Connect to the bus */
309 r
= manager_connect_bus(m
);
313 /* Deserialize state */
314 manager_enumerate_machines(m
);
316 /* Remove stale objects before we start them */
317 manager_gc(m
, false);
319 /* And start everything */
320 HASHMAP_FOREACH(machine
, m
->machines
, i
)
321 machine_start(machine
, NULL
, NULL
);
326 static bool check_idle(void *userdata
) {
327 Manager
*m
= userdata
;
334 return hashmap_isempty(m
->machines
);
337 static int manager_run(Manager
*m
) {
340 return bus_event_loop_with_idle(
343 "org.freedesktop.machine1",
348 static int run(int argc
, char *argv
[]) {
349 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
352 log_set_facility(LOG_AUTH
);
358 log_error("This program takes no arguments.");
362 /* Always create the directories people can create inotify watches in. Note that some applications might check
363 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
364 * make sure this check stays in. */
365 (void) mkdir_label("/run/systemd/machines", 0755);
367 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
371 return log_error_errno(r
, "Failed to allocate manager object: %m");
373 r
= manager_startup(m
);
375 return log_error_errno(r
, "Failed to fully start up daemon: %m");
377 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
378 (void) sd_notify(false,
380 "STATUS=Processing requests...");
384 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
385 (void) sd_notify(false,
387 "STATUS=Shutting down...");
392 DEFINE_MAIN_FUNCTION(run
);