]>
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
);
81 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
84 sd_event_unref(m
->event
);
89 static int manager_add_host_machine(Manager
*m
) {
90 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
98 r
= sd_id128_get_machine(&mid
);
100 return log_error_errno(r
, "Failed to get machine ID: %m");
106 unit
= strdup(SPECIAL_ROOT_SLICE
);
110 t
= machine_new(m
, MACHINE_HOST
, ".host");
117 t
->root_directory
= TAKE_PTR(rd
);
118 t
->unit
= TAKE_PTR(unit
);
120 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
127 static int manager_enumerate_machines(Manager
*m
) {
128 _cleanup_closedir_
DIR *d
= NULL
;
134 r
= manager_add_host_machine(m
);
138 /* Read in machine data stored on disk */
139 d
= opendir("/run/systemd/machines");
144 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
147 FOREACH_DIRENT(de
, d
, return -errno
) {
148 struct Machine
*machine
;
151 if (!dirent_is_file(de
))
154 /* Ignore symlinks that map the unit name to the machine */
155 if (startswith(de
->d_name
, "unit:"))
158 if (!machine_name_is_valid(de
->d_name
))
161 k
= manager_add_machine(m
, de
->d_name
, &machine
);
163 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
167 machine_add_to_gc_queue(machine
);
169 k
= machine_load(machine
);
177 static int manager_connect_bus(Manager
*m
) {
183 r
= sd_bus_default_system(&m
->bus
);
185 return log_error_errno(r
, "Failed to connect to system bus: %m");
187 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
189 return log_error_errno(r
, "Failed to add manager object vtable: %m");
191 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
193 return log_error_errno(r
, "Failed to add machine object vtable: %m");
195 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
197 return log_error_errno(r
, "Failed to add machine enumerator: %m");
199 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
201 return log_error_errno(r
, "Failed to add image object vtable: %m");
203 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
205 return log_error_errno(r
, "Failed to add image enumerator: %m");
207 r
= sd_bus_match_signal_async(
210 "org.freedesktop.systemd1",
211 "/org/freedesktop/systemd1",
212 "org.freedesktop.systemd1.Manager",
214 match_job_removed
, NULL
, m
);
216 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
218 r
= sd_bus_match_signal_async(
221 "org.freedesktop.systemd1",
222 "/org/freedesktop/systemd1",
223 "org.freedesktop.systemd1.Manager",
225 match_unit_removed
, NULL
, m
);
227 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
229 r
= sd_bus_match_signal_async(
232 "org.freedesktop.systemd1",
234 "org.freedesktop.DBus.Properties",
236 match_properties_changed
, NULL
, m
);
238 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
240 r
= sd_bus_match_signal_async(
243 "org.freedesktop.systemd1",
244 "/org/freedesktop/systemd1",
245 "org.freedesktop.systemd1.Manager",
247 match_reloading
, NULL
, m
);
249 return log_error_errno(r
, "Failed to request match for Reloading: %m");
251 r
= sd_bus_call_method_async(
254 "org.freedesktop.systemd1",
255 "/org/freedesktop/systemd1",
256 "org.freedesktop.systemd1.Manager",
261 return log_error_errno(r
, "Failed to enable subscription: %m");
263 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
265 return log_error_errno(r
, "Failed to request name: %m");
267 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
269 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
274 static void manager_gc(Manager
*m
, bool drop_not_started
) {
279 while ((machine
= m
->machine_gc_queue
)) {
280 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
281 machine
->in_gc_queue
= false;
283 /* First, if we are not closing yet, initiate stopping */
284 if (machine_may_gc(machine
, drop_not_started
) &&
285 machine_get_state(machine
) != MACHINE_CLOSING
)
286 machine_stop(machine
);
288 /* Now, the stop probably made this referenced
289 * again, but if it didn't, then it's time to let it
291 if (machine_may_gc(machine
, drop_not_started
)) {
292 machine_finalize(machine
);
293 machine_free(machine
);
298 static int manager_startup(Manager
*m
) {
305 /* Connect to the bus */
306 r
= manager_connect_bus(m
);
310 /* Deserialize state */
311 manager_enumerate_machines(m
);
313 /* Remove stale objects before we start them */
314 manager_gc(m
, false);
316 /* And start everything */
317 HASHMAP_FOREACH(machine
, m
->machines
, i
)
318 machine_start(machine
, NULL
, NULL
);
323 static bool check_idle(void *userdata
) {
324 Manager
*m
= userdata
;
331 return hashmap_isempty(m
->machines
);
334 static int manager_run(Manager
*m
) {
337 return bus_event_loop_with_idle(
340 "org.freedesktop.machine1",
345 static int run(int argc
, char *argv
[]) {
346 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
349 log_set_facility(LOG_AUTH
);
355 log_error("This program takes no arguments.");
359 /* Always create the directories people can create inotify watches in. Note that some applications might check
360 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
361 * make sure this check stays in. */
362 (void) mkdir_label("/run/systemd/machines", 0755);
364 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
368 return log_error_errno(r
, "Failed to allocate manager object: %m");
370 r
= manager_startup(m
);
372 return log_error_errno(r
, "Failed to fully start up daemon: %m");
374 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
375 (void) sd_notify(false,
377 "STATUS=Processing requests...");
381 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
382 (void) sd_notify(false,
384 "STATUS=Shutting down...");
389 DEFINE_MAIN_FUNCTION(run
);