]>
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
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "sd-daemon.h"
27 #include "alloc-util.h"
28 #include "bus-error.h"
30 #include "cgroup-util.h"
31 #include "dirent-util.h"
33 #include "format-util.h"
34 #include "hostname-util.h"
36 #include "machine-image.h"
38 #include "process-util.h"
39 #include "signal-util.h"
42 Manager
*manager_new(void) {
50 m
->machines
= hashmap_new(&string_hash_ops
);
51 m
->machine_units
= hashmap_new(&string_hash_ops
);
52 m
->machine_leaders
= hashmap_new(NULL
);
54 if (!m
->machines
|| !m
->machine_units
|| !m
->machine_leaders
) {
59 r
= sd_event_default(&m
->event
);
65 sd_event_set_watchdog(m
->event
, true);
70 void manager_free(Manager
*m
) {
76 operation_free(m
->operations
);
78 assert(m
->n_operations
== 0);
80 while ((machine
= hashmap_first(m
->machines
)))
81 machine_free(machine
);
83 hashmap_free(m
->machines
);
84 hashmap_free(m
->machine_units
);
85 hashmap_free(m
->machine_leaders
);
87 hashmap_free_with_destructor(m
->image_cache
, image_unref
);
89 sd_event_source_unref(m
->image_cache_defer_event
);
91 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
94 sd_event_unref(m
->event
);
99 static int manager_add_host_machine(Manager
*m
) {
100 _cleanup_free_
char *rd
= NULL
, *unit
= NULL
;
108 r
= sd_id128_get_machine(&mid
);
110 return log_error_errno(r
, "Failed to get machine ID: %m");
116 unit
= strdup(SPECIAL_ROOT_SLICE
);
120 t
= machine_new(m
, MACHINE_HOST
, ".host");
127 t
->root_directory
= rd
;
131 dual_timestamp_from_boottime_or_monotonic(&t
->timestamp
, 0);
138 int manager_enumerate_machines(Manager
*m
) {
139 _cleanup_closedir_
DIR *d
= NULL
;
145 r
= manager_add_host_machine(m
);
149 /* Read in machine data stored on disk */
150 d
= opendir("/run/systemd/machines");
155 return log_error_errno(errno
, "Failed to open /run/systemd/machines: %m");
158 FOREACH_DIRENT(de
, d
, return -errno
) {
159 struct Machine
*machine
;
162 if (!dirent_is_file(de
))
165 /* Ignore symlinks that map the unit name to the machine */
166 if (startswith(de
->d_name
, "unit:"))
169 if (!machine_name_is_valid(de
->d_name
))
172 k
= manager_add_machine(m
, de
->d_name
, &machine
);
174 r
= log_error_errno(k
, "Failed to add machine by file name %s: %m", de
->d_name
);
178 machine_add_to_gc_queue(machine
);
180 k
= machine_load(machine
);
188 static int manager_connect_bus(Manager
*m
) {
189 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
195 r
= sd_bus_default_system(&m
->bus
);
197 return log_error_errno(r
, "Failed to connect to system bus: %m");
199 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable
, m
);
201 return log_error_errno(r
, "Failed to add manager object vtable: %m");
203 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable
, machine_object_find
, m
);
205 return log_error_errno(r
, "Failed to add machine object vtable: %m");
207 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/machine", machine_node_enumerator
, m
);
209 return log_error_errno(r
, "Failed to add machine enumerator: %m");
211 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable
, image_object_find
, m
);
213 return log_error_errno(r
, "Failed to add image object vtable: %m");
215 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/machine1/image", image_node_enumerator
, m
);
217 return log_error_errno(r
, "Failed to add image enumerator: %m");
219 r
= sd_bus_add_match(m
->bus
,
222 "sender='org.freedesktop.systemd1',"
223 "interface='org.freedesktop.systemd1.Manager',"
224 "member='JobRemoved',"
225 "path='/org/freedesktop/systemd1'",
229 return log_error_errno(r
, "Failed to add match for JobRemoved: %m");
231 r
= sd_bus_add_match(m
->bus
,
234 "sender='org.freedesktop.systemd1',"
235 "interface='org.freedesktop.systemd1.Manager',"
236 "member='UnitRemoved',"
237 "path='/org/freedesktop/systemd1'",
241 return log_error_errno(r
, "Failed to add match for UnitRemoved: %m");
243 r
= sd_bus_add_match(m
->bus
,
246 "sender='org.freedesktop.systemd1',"
247 "interface='org.freedesktop.DBus.Properties',"
248 "member='PropertiesChanged',"
249 "arg0='org.freedesktop.systemd1.Unit'",
250 match_properties_changed
,
253 return log_error_errno(r
, "Failed to add match for PropertiesChanged: %m");
255 r
= sd_bus_add_match(m
->bus
,
258 "sender='org.freedesktop.systemd1',"
259 "interface='org.freedesktop.systemd1.Manager',"
260 "member='Reloading',"
261 "path='/org/freedesktop/systemd1'",
265 return log_error_errno(r
, "Failed to add match for Reloading: %m");
267 r
= sd_bus_call_method(
269 "org.freedesktop.systemd1",
270 "/org/freedesktop/systemd1",
271 "org.freedesktop.systemd1.Manager",
276 log_error("Failed to enable subscription: %s", bus_error_message(&error
, r
));
280 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.machine1", 0, NULL
, NULL
);
282 return log_error_errno(r
, "Failed to request name: %m");
284 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
286 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
291 void manager_gc(Manager
*m
, bool drop_not_started
) {
296 while ((machine
= m
->machine_gc_queue
)) {
297 LIST_REMOVE(gc_queue
, m
->machine_gc_queue
, machine
);
298 machine
->in_gc_queue
= false;
300 /* First, if we are not closing yet, initiate stopping */
301 if (!machine_check_gc(machine
, drop_not_started
) &&
302 machine_get_state(machine
) != MACHINE_CLOSING
)
303 machine_stop(machine
);
305 /* Now, the stop probably made this referenced
306 * again, but if it didn't, then it's time to let it
308 if (!machine_check_gc(machine
, drop_not_started
)) {
309 machine_finalize(machine
);
310 machine_free(machine
);
315 int manager_startup(Manager
*m
) {
322 /* Connect to the bus */
323 r
= manager_connect_bus(m
);
327 /* Deserialize state */
328 manager_enumerate_machines(m
);
330 /* Remove stale objects before we start them */
331 manager_gc(m
, false);
333 /* And start everything */
334 HASHMAP_FOREACH(machine
, m
->machines
, i
)
335 machine_start(machine
, NULL
, NULL
);
340 static bool check_idle(void *userdata
) {
341 Manager
*m
= userdata
;
348 return hashmap_isempty(m
->machines
);
351 int manager_run(Manager
*m
) {
354 return bus_event_loop_with_idle(
357 "org.freedesktop.machine1",
362 int main(int argc
, char *argv
[]) {
366 log_set_target(LOG_TARGET_AUTO
);
367 log_set_facility(LOG_AUTH
);
368 log_parse_environment();
374 log_error("This program takes no arguments.");
379 /* Always create the directories people can create inotify
380 * watches in. Note that some applications might check for the
381 * existence of /run/systemd/machines/ to determine whether
382 * machined is available, so please always make sure this
384 mkdir_label("/run/systemd/machines", 0755);
386 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
394 r
= manager_startup(m
);
396 log_error_errno(r
, "Failed to fully start up daemon: %m");
400 log_debug("systemd-machined running as pid "PID_FMT
, getpid_cached());
404 "STATUS=Processing requests...");
408 log_debug("systemd-machined stopped as pid "PID_FMT
, getpid_cached());
413 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;