]>
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 static int manager_new(Manager
**ret
) {
29 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
38 m
->machines
= hashmap_new(&string_hash_ops
);
39 m
->machine_units
= hashmap_new(&string_hash_ops
);
40 m
->machine_leaders
= hashmap_new(NULL
);
42 if (!m
->machines
|| !m
->machine_units
|| !m
->machine_leaders
)
45 r
= sd_event_default(&m
->event
);
49 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
53 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
57 (void) sd_event_set_watchdog(m
->event
, true);
63 static Manager
* manager_unref(Manager
*m
) {
70 operation_free(m
->operations
);
72 assert(m
->n_operations
== 0);
74 while ((machine
= hashmap_first(m
->machines
)))
75 machine_free(machine
);
77 hashmap_free(m
->machines
);
78 hashmap_free(m
->machine_units
);
79 hashmap_free(m
->machine_leaders
);
81 hashmap_free_with_destructor(m
->image_cache
, image_unref
);
83 sd_event_source_unref(m
->image_cache_defer_event
);
85 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
88 sd_event_unref(m
->event
);
93 static int manager_add_host_machine(Manager
*m
) {
94 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
102 r
= sd_id128_get_machine(&mid
);
104 return log_error_errno(r
, "Failed to get machine ID: %m");
110 unit
= strdup(SPECIAL_ROOT_SLICE
);
114 t
= machine_new(m
, MACHINE_HOST
, ".host");
121 t
->root_directory
= TAKE_PTR(rd
);
122 t
->unit
= TAKE_PTR(unit
);
124 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
131 static int manager_enumerate_machines(Manager
*m
) {
132 _cleanup_closedir_
DIR *d
= NULL
;
138 r
= manager_add_host_machine(m
);
142 /* Read in machine data stored on disk */
143 d
= opendir("/run/systemd/machines");
148 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
151 FOREACH_DIRENT(de
, d
, return -errno
) {
152 struct Machine
*machine
;
155 if (!dirent_is_file(de
))
158 /* Ignore symlinks that map the unit name to the machine */
159 if (startswith(de
->d_name
, "unit:"))
162 if (!machine_name_is_valid(de
->d_name
))
165 k
= manager_add_machine(m
, de
->d_name
, &machine
);
167 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
171 machine_add_to_gc_queue(machine
);
173 k
= machine_load(machine
);
181 static int manager_connect_bus(Manager
*m
) {
187 r
= sd_bus_default_system(&m
->bus
);
189 return log_error_errno(r
, "Failed to connect to system bus: %m");
191 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
193 return log_error_errno(r
, "Failed to add manager object vtable: %m");
195 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
197 return log_error_errno(r
, "Failed to add machine object vtable: %m");
199 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
201 return log_error_errno(r
, "Failed to add machine enumerator: %m");
203 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
205 return log_error_errno(r
, "Failed to add image object vtable: %m");
207 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
209 return log_error_errno(r
, "Failed to add image enumerator: %m");
211 r
= sd_bus_match_signal_async(
214 "org.freedesktop.systemd1",
215 "/org/freedesktop/systemd1",
216 "org.freedesktop.systemd1.Manager",
218 match_job_removed
, NULL
, m
);
220 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
222 r
= sd_bus_match_signal_async(
225 "org.freedesktop.systemd1",
226 "/org/freedesktop/systemd1",
227 "org.freedesktop.systemd1.Manager",
229 match_unit_removed
, NULL
, m
);
231 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
233 r
= sd_bus_match_signal_async(
236 "org.freedesktop.systemd1",
238 "org.freedesktop.DBus.Properties",
240 match_properties_changed
, NULL
, m
);
242 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
244 r
= sd_bus_match_signal_async(
247 "org.freedesktop.systemd1",
248 "/org/freedesktop/systemd1",
249 "org.freedesktop.systemd1.Manager",
251 match_reloading
, NULL
, m
);
253 return log_error_errno(r
, "Failed to request match for Reloading: %m");
255 r
= sd_bus_call_method_async(
258 "org.freedesktop.systemd1",
259 "/org/freedesktop/systemd1",
260 "org.freedesktop.systemd1.Manager",
265 return log_error_errno(r
, "Failed to enable subscription: %m");
267 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
269 return log_error_errno(r
, "Failed to request name: %m");
271 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
273 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
278 static void manager_gc(Manager
*m
, bool drop_not_started
) {
283 while ((machine
= m
->machine_gc_queue
)) {
284 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
285 machine
->in_gc_queue
= false;
287 /* First, if we are not closing yet, initiate stopping */
288 if (machine_may_gc(machine
, drop_not_started
) &&
289 machine_get_state(machine
) != MACHINE_CLOSING
)
290 machine_stop(machine
);
292 /* Now, the stop probably made this referenced
293 * again, but if it didn't, then it's time to let it
295 if (machine_may_gc(machine
, drop_not_started
)) {
296 machine_finalize(machine
);
297 machine_free(machine
);
302 static int manager_startup(Manager
*m
) {
309 /* Connect to the bus */
310 r
= manager_connect_bus(m
);
314 /* Deserialize state */
315 manager_enumerate_machines(m
);
317 /* Remove stale objects before we start them */
318 manager_gc(m
, false);
320 /* And start everything */
321 HASHMAP_FOREACH(machine
, m
->machines
, i
)
322 machine_start(machine
, NULL
, NULL
);
327 static bool check_idle(void *userdata
) {
328 Manager
*m
= userdata
;
335 return hashmap_isempty(m
->machines
);
338 static int manager_run(Manager
*m
) {
341 return bus_event_loop_with_idle(
344 "org.freedesktop.machine1",
349 static int run(int argc
, char *argv
[]) {
350 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
353 log_set_facility(LOG_AUTH
);
359 log_error("This program takes no arguments.");
363 /* Always create the directories people can create inotify watches in. Note that some applications might check
364 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
365 * make sure this check stays in. */
366 (void) mkdir_label("/run/systemd/machines", 0755);
368 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
372 return log_error_errno(r
, "Failed to allocate manager object: %m");
374 r
= manager_startup(m
);
376 return log_error_errno(r
, "Failed to fully start up daemon: %m");
378 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
379 (void) sd_notify(false,
381 "STATUS=Processing requests...");
385 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
386 (void) sd_notify(false,
388 "STATUS=Shutting down...");
393 DEFINE_MAIN_FUNCTION(run
);