]>
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 Manager
*manager_new(void) {
37 m
->machines
= hashmap_new(&string_hash_ops
);
38 m
->machine_units
= hashmap_new(&string_hash_ops
);
39 m
->machine_leaders
= hashmap_new(NULL
);
41 if (!m
->machines
|| !m
->machine_units
|| !m
->machine_leaders
) {
46 r
= sd_event_default(&m
->event
);
52 sd_event_set_watchdog(m
->event
, true);
57 void manager_free(Manager
*m
) {
63 operation_free(m
->operations
);
65 assert(m
->n_operations
== 0);
67 while ((machine
= hashmap_first(m
->machines
)))
68 machine_free(machine
);
70 hashmap_free(m
->machines
);
71 hashmap_free(m
->machine_units
);
72 hashmap_free(m
->machine_leaders
);
74 hashmap_free_with_destructor(m
->image_cache
, image_unref
);
76 sd_event_source_unref(m
->image_cache_defer_event
);
78 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
81 sd_event_unref(m
->event
);
86 static int manager_add_host_machine(Manager
*m
) {
87 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
95 r
= sd_id128_get_machine(&mid
);
97 return log_error_errno(r
, "Failed to get machine ID: %m");
103 unit
= strdup(SPECIAL_ROOT_SLICE
);
107 t
= machine_new(m
, MACHINE_HOST
, ".host");
114 t
->root_directory
= TAKE_PTR(rd
);
115 t
->unit
= TAKE_PTR(unit
);
117 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
124 int manager_enumerate_machines(Manager
*m
) {
125 _cleanup_closedir_
DIR *d
= NULL
;
131 r
= manager_add_host_machine(m
);
135 /* Read in machine data stored on disk */
136 d
= opendir("/run/systemd/machines");
141 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
144 FOREACH_DIRENT(de
, d
, return -errno
) {
145 struct Machine
*machine
;
148 if (!dirent_is_file(de
))
151 /* Ignore symlinks that map the unit name to the machine */
152 if (startswith(de
->d_name
, "unit:"))
155 if (!machine_name_is_valid(de
->d_name
))
158 k
= manager_add_machine(m
, de
->d_name
, &machine
);
160 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
164 machine_add_to_gc_queue(machine
);
166 k
= machine_load(machine
);
174 static int manager_connect_bus(Manager
*m
) {
180 r
= sd_bus_default_system(&m
->bus
);
182 return log_error_errno(r
, "Failed to connect to system bus: %m");
184 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
186 return log_error_errno(r
, "Failed to add manager object vtable: %m");
188 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
190 return log_error_errno(r
, "Failed to add machine object vtable: %m");
192 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
194 return log_error_errno(r
, "Failed to add machine enumerator: %m");
196 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
198 return log_error_errno(r
, "Failed to add image object vtable: %m");
200 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
202 return log_error_errno(r
, "Failed to add image enumerator: %m");
204 r
= sd_bus_match_signal_async(
207 "org.freedesktop.systemd1",
208 "/org/freedesktop/systemd1",
209 "org.freedesktop.systemd1.Manager",
211 match_job_removed
, NULL
, m
);
213 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
215 r
= sd_bus_match_signal_async(
218 "org.freedesktop.systemd1",
219 "/org/freedesktop/systemd1",
220 "org.freedesktop.systemd1.Manager",
222 match_unit_removed
, NULL
, m
);
224 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
226 r
= sd_bus_match_signal_async(
229 "org.freedesktop.systemd1",
231 "org.freedesktop.DBus.Properties",
233 match_properties_changed
, NULL
, m
);
235 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
237 r
= sd_bus_match_signal_async(
240 "org.freedesktop.systemd1",
241 "/org/freedesktop/systemd1",
242 "org.freedesktop.systemd1.Manager",
244 match_reloading
, NULL
, m
);
246 return log_error_errno(r
, "Failed to request match for Reloading: %m");
248 r
= sd_bus_call_method_async(
251 "org.freedesktop.systemd1",
252 "/org/freedesktop/systemd1",
253 "org.freedesktop.systemd1.Manager",
258 return log_error_errno(r
, "Failed to enable subscription: %m");
260 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
262 return log_error_errno(r
, "Failed to request name: %m");
264 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
266 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
271 void manager_gc(Manager
*m
, bool drop_not_started
) {
276 while ((machine
= m
->machine_gc_queue
)) {
277 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
278 machine
->in_gc_queue
= false;
280 /* First, if we are not closing yet, initiate stopping */
281 if (machine_may_gc(machine
, drop_not_started
) &&
282 machine_get_state(machine
) != MACHINE_CLOSING
)
283 machine_stop(machine
);
285 /* Now, the stop probably made this referenced
286 * again, but if it didn't, then it's time to let it
288 if (machine_may_gc(machine
, drop_not_started
)) {
289 machine_finalize(machine
);
290 machine_free(machine
);
295 int manager_startup(Manager
*m
) {
302 /* Connect to the bus */
303 r
= manager_connect_bus(m
);
307 /* Deserialize state */
308 manager_enumerate_machines(m
);
310 /* Remove stale objects before we start them */
311 manager_gc(m
, false);
313 /* And start everything */
314 HASHMAP_FOREACH(machine
, m
->machines
, i
)
315 machine_start(machine
, NULL
, NULL
);
320 static bool check_idle(void *userdata
) {
321 Manager
*m
= userdata
;
328 return hashmap_isempty(m
->machines
);
331 int manager_run(Manager
*m
) {
334 return bus_event_loop_with_idle(
337 "org.freedesktop.machine1",
342 int main(int argc
, char *argv
[]) {
346 log_set_target(LOG_TARGET_AUTO
);
347 log_set_facility(LOG_AUTH
);
348 log_parse_environment();
354 log_error("This program takes no arguments.");
359 /* Always create the directories people can create inotify
360 * watches in. Note that some applications might check for the
361 * existence of /run/systemd/machines/ to determine whether
362 * machined is available, so please always make sure this
364 mkdir_label("/run/systemd/machines", 0755);
366 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
374 r
= manager_startup(m
);
376 log_error_errno(r
, "Failed to fully start up daemon: %m");
380 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
384 "STATUS=Processing requests...");
388 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
393 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;