]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
10 #include "sd-daemon.h"
12 #include "alloc-util.h"
13 #include "bus-error.h"
15 #include "cgroup-util.h"
16 #include "dirent-util.h"
18 #include "format-util.h"
19 #include "hostname-util.h"
21 #include "machine-image.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 static int manager_new(Manager
**ret
) {
31 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
40 m
->machines
= hashmap_new(&string_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
) {
72 operation_free(m
->operations
);
74 assert(m
->n_operations
== 0);
76 while ((machine
= hashmap_first(m
->machines
)))
77 machine_free(machine
);
79 hashmap_free(m
->machines
);
80 hashmap_free(m
->machine_units
);
81 hashmap_free(m
->machine_leaders
);
83 hashmap_free_with_destructor(m
->image_cache
, image_unref
);
85 sd_event_source_unref(m
->image_cache_defer_event
);
87 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
90 sd_event_unref(m
->event
);
95 static int manager_add_host_machine(Manager
*m
) {
96 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
104 r
= sd_id128_get_machine(&mid
);
106 return log_error_errno(r
, "Failed to get machine ID: %m");
112 unit
= strdup(SPECIAL_ROOT_SLICE
);
116 t
= machine_new(m
, MACHINE_HOST
, ".host");
123 t
->root_directory
= TAKE_PTR(rd
);
124 t
->unit
= TAKE_PTR(unit
);
126 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
133 static int manager_enumerate_machines(Manager
*m
) {
134 _cleanup_closedir_
DIR *d
= NULL
;
140 r
= manager_add_host_machine(m
);
144 /* Read in machine data stored on disk */
145 d
= opendir("/run/systemd/machines");
150 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
153 FOREACH_DIRENT(de
, d
, return -errno
) {
154 struct Machine
*machine
;
157 if (!dirent_is_file(de
))
160 /* Ignore symlinks that map the unit name to the machine */
161 if (startswith(de
->d_name
, "unit:"))
164 if (!machine_name_is_valid(de
->d_name
))
167 k
= manager_add_machine(m
, de
->d_name
, &machine
);
169 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
173 machine_add_to_gc_queue(machine
);
175 k
= machine_load(machine
);
183 static int manager_connect_bus(Manager
*m
) {
189 r
= sd_bus_default_system(&m
->bus
);
191 return log_error_errno(r
, "Failed to connect to system bus: %m");
193 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
195 return log_error_errno(r
, "Failed to add manager object vtable: %m");
197 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
199 return log_error_errno(r
, "Failed to add machine object vtable: %m");
201 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
203 return log_error_errno(r
, "Failed to add machine enumerator: %m");
205 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
207 return log_error_errno(r
, "Failed to add image object vtable: %m");
209 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
211 return log_error_errno(r
, "Failed to add image enumerator: %m");
213 r
= sd_bus_match_signal_async(
216 "org.freedesktop.systemd1",
217 "/org/freedesktop/systemd1",
218 "org.freedesktop.systemd1.Manager",
220 match_job_removed
, NULL
, m
);
222 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
224 r
= sd_bus_match_signal_async(
227 "org.freedesktop.systemd1",
228 "/org/freedesktop/systemd1",
229 "org.freedesktop.systemd1.Manager",
231 match_unit_removed
, NULL
, m
);
233 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
235 r
= sd_bus_match_signal_async(
238 "org.freedesktop.systemd1",
240 "org.freedesktop.DBus.Properties",
242 match_properties_changed
, NULL
, m
);
244 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
246 r
= sd_bus_match_signal_async(
249 "org.freedesktop.systemd1",
250 "/org/freedesktop/systemd1",
251 "org.freedesktop.systemd1.Manager",
253 match_reloading
, NULL
, m
);
255 return log_error_errno(r
, "Failed to request match for Reloading: %m");
257 r
= sd_bus_call_method_async(
260 "org.freedesktop.systemd1",
261 "/org/freedesktop/systemd1",
262 "org.freedesktop.systemd1.Manager",
267 return log_error_errno(r
, "Failed to enable subscription: %m");
269 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
271 return log_error_errno(r
, "Failed to request name: %m");
273 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
275 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
280 static void manager_gc(Manager
*m
, bool drop_not_started
) {
285 while ((machine
= m
->machine_gc_queue
)) {
286 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
287 machine
->in_gc_queue
= false;
289 /* First, if we are not closing yet, initiate stopping */
290 if (machine_may_gc(machine
, drop_not_started
) &&
291 machine_get_state(machine
) != MACHINE_CLOSING
)
292 machine_stop(machine
);
294 /* Now, the stop probably made this referenced
295 * again, but if it didn't, then it's time to let it
297 if (machine_may_gc(machine
, drop_not_started
)) {
298 machine_finalize(machine
);
299 machine_free(machine
);
304 static int manager_startup(Manager
*m
) {
311 /* Connect to the bus */
312 r
= manager_connect_bus(m
);
316 /* Deserialize state */
317 manager_enumerate_machines(m
);
319 /* Remove stale objects before we start them */
320 manager_gc(m
, false);
322 /* And start everything */
323 HASHMAP_FOREACH(machine
, m
->machines
, i
)
324 machine_start(machine
, NULL
, NULL
);
329 static bool check_idle(void *userdata
) {
330 Manager
*m
= userdata
;
337 return hashmap_isempty(m
->machines
);
340 static int manager_run(Manager
*m
) {
343 return bus_event_loop_with_idle(
346 "org.freedesktop.machine1",
351 int main(int argc
, char *argv
[]) {
352 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
355 log_set_target(LOG_TARGET_AUTO
);
356 log_set_facility(LOG_AUTH
);
357 log_parse_environment();
363 log_error("This program takes no arguments.");
368 /* Always create the directories people can create inotify watches in. Note that some applications might check
369 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
370 * make sure this check stays in. */
371 (void) mkdir_label("/run/systemd/machines", 0755);
373 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGTERM
, SIGINT
, -1) >= 0);
377 log_error_errno(r
, "Failed to allocate manager object: %m");
381 r
= manager_startup(m
);
383 log_error_errno(r
, "Failed to fully start up daemon: %m");
387 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
389 (void) sd_notify(false,
391 "STATUS=Processing requests...");
395 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
397 (void) sd_notify(false,
399 "STATUS=Shutting down...");
402 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;