]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machined.c
Define FOREACH_DIRENT through FOREACH_DIRENT_ALL
[thirdparty/systemd.git] / src / machine / machined.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
1ee306e1
LP
2
3#include <errno.h>
1ee306e1 4#include <string.h>
ca78ad1d
ZJS
5#include <sys/stat.h>
6#include <sys/types.h>
1ee306e1 7#include <unistd.h>
1ee306e1 8
b5efdb8a 9#include "alloc-util.h"
c3350683 10#include "bus-error.h"
9b71e4ab 11#include "bus-locator.h"
ac9f55ed 12#include "bus-log-control-api.h"
269e4d2d 13#include "bus-polkit.h"
3ffd4af2 14#include "cgroup-util.h"
4bf4f50f 15#include "daemon-util.h"
a0956174 16#include "dirent-util.h"
57f1b61b 17#include "discover-image.h"
3ffd4af2 18#include "fd-util.h"
f97b34a6 19#include "format-util.h"
25300b5a 20#include "hostname-util.h"
4751364e 21#include "machined-varlink.h"
ebeccf9e 22#include "machined.h"
5e332028 23#include "main-func.h"
35cd0ba5 24#include "mkdir-label.h"
df0ff127 25#include "process-util.h"
fc021a5b 26#include "service-util.h"
3ffd4af2 27#include "signal-util.h"
e5af6e0e 28#include "special.h"
1ee306e1 29
730fa7ce
LP
30static Manager* manager_unref(Manager *m);
31DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
32
5be61bea 33DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops, char, string_hash_func, string_compare_func, Machine, machine_free);
bb1a05d6 34
730fa7ce
LP
35static int manager_new(Manager **ret) {
36 _cleanup_(manager_unrefp) Manager *m = NULL;
c3350683 37 int r;
1ee306e1 38
730fa7ce
LP
39 assert(ret);
40
1ee306e1
LP
41 m = new0(Manager, 1);
42 if (!m)
730fa7ce 43 return -ENOMEM;
1ee306e1 44
5be61bea 45 m->machines = hashmap_new(&machine_hash_ops);
d5099efc 46 m->machine_units = hashmap_new(&string_hash_ops);
5be61bea 47 m->machine_leaders = hashmap_new(NULL);
1ee306e1 48
730fa7ce
LP
49 if (!m->machines || !m->machine_units || !m->machine_leaders)
50 return -ENOMEM;
c3350683 51
afc6adb5 52 r = sd_event_default(&m->event);
730fa7ce
LP
53 if (r < 0)
54 return r;
55
56 r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
57 if (r < 0)
58 return r;
59
60 r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
61 if (r < 0)
62 return r;
1ee306e1 63
730fa7ce 64 (void) sd_event_set_watchdog(m->event, true);
cde93897 65
730fa7ce
LP
66 *ret = TAKE_PTR(m);
67 return 0;
1ee306e1
LP
68}
69
730fa7ce 70static Manager* manager_unref(Manager *m) {
c8f05436
LP
71 if (!m)
72 return NULL;
1ee306e1 73
56599585
LP
74 while (m->operations)
75 operation_free(m->operations);
76
77 assert(m->n_operations == 0);
78
5be61bea 79 hashmap_free(m->machines); /* This will free all machines, so that the machine_units/machine_leaders is empty */
1ee306e1 80 hashmap_free(m->machine_units);
d3e84ddb 81 hashmap_free(m->machine_leaders);
b07ec5a1 82 hashmap_free(m->image_cache);
1ddb263d
LP
83
84 sd_event_source_unref(m->image_cache_defer_event);
7e0079f9 85#if ENABLE_NSCD
9fdcbae5 86 sd_event_source_unref(m->nscd_cache_flush_event);
7e0079f9 87#endif
1ddb263d 88
d04c1fb8
LP
89 bus_verify_polkit_async_registry_free(m->polkit_registry);
90
4751364e
LP
91 manager_varlink_done(m);
92
92e31da1 93 sd_bus_flush_close_unref(m->bus);
c3350683
LP
94 sd_event_unref(m->event);
95
730fa7ce 96 return mfree(m);
c3350683
LP
97}
98
fbe55073
LP
99static int manager_add_host_machine(Manager *m) {
100 _cleanup_free_ char *rd = NULL, *unit = NULL;
101 sd_id128_t mid;
102 Machine *t;
103 int r;
104
105 if (m->host_machine)
106 return 0;
107
108 r = sd_id128_get_machine(&mid);
109 if (r < 0)
110 return log_error_errno(r, "Failed to get machine ID: %m");
111
112 rd = strdup("/");
113 if (!rd)
114 return log_oom();
115
e5af6e0e 116 unit = strdup(SPECIAL_ROOT_SLICE);
fbe55073
LP
117 if (!unit)
118 return log_oom();
119
120 t = machine_new(m, MACHINE_HOST, ".host");
121 if (!t)
122 return log_oom();
123
124 t->leader = 1;
125 t->id = mid;
126
1cc6c93a
YW
127 t->root_directory = TAKE_PTR(rd);
128 t->unit = TAKE_PTR(unit);
fbe55073
LP
129
130 dual_timestamp_from_boottime_or_monotonic(&t->timestamp, 0);
131
132 m->host_machine = t;
133
134 return 0;
135}
136
730fa7ce 137static int manager_enumerate_machines(Manager *m) {
1ee306e1 138 _cleanup_closedir_ DIR *d = NULL;
6d95e7d9 139 int r;
1ee306e1
LP
140
141 assert(m);
142
fbe55073
LP
143 r = manager_add_host_machine(m);
144 if (r < 0)
145 return r;
146
1ee306e1
LP
147 /* Read in machine data stored on disk */
148 d = opendir("/run/systemd/machines");
149 if (!d) {
150 if (errno == ENOENT)
151 return 0;
152
e1427b13 153 return log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
1ee306e1
LP
154 }
155
156 FOREACH_DIRENT(de, d, return -errno) {
157 struct Machine *machine;
158 int k;
159
160 if (!dirent_is_file(de))
161 continue;
162
b87633c4
LP
163 /* Ignore symlinks that map the unit name to the machine */
164 if (startswith(de->d_name, "unit:"))
165 continue;
166
52ef5dd7 167 if (!hostname_is_valid(de->d_name, 0))
b9a8d250
LP
168 continue;
169
1ee306e1
LP
170 k = manager_add_machine(m, de->d_name, &machine);
171 if (k < 0) {
fbe55073 172 r = log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
1ee306e1
LP
173 continue;
174 }
175
176 machine_add_to_gc_queue(machine);
177
178 k = machine_load(machine);
179 if (k < 0)
180 r = k;
181 }
182
183 return r;
184}
185
1ee306e1 186static int manager_connect_bus(Manager *m) {
1ee306e1 187 int r;
1ee306e1
LP
188
189 assert(m);
190 assert(!m->bus);
1ee306e1 191
76b54375 192 r = sd_bus_default_system(&m->bus);
f647962d
MS
193 if (r < 0)
194 return log_error_errno(r, "Failed to connect to system bus: %m");
1ee306e1 195
4faa530c 196 r = bus_add_implementation(m->bus, &manager_object, m);
f647962d 197 if (r < 0)
4faa530c 198 return r;
ebeccf9e 199
14456f76 200 r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "JobRemoved", match_job_removed, NULL, m);
f647962d
MS
201 if (r < 0)
202 return log_error_errno(r, "Failed to add match for JobRemoved: %m");
1ee306e1 203
14456f76 204 r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "UnitRemoved", match_unit_removed, NULL, m);
f647962d 205 if (r < 0)
75152a4d
LP
206 return log_error_errno(r, "Failed to request match for UnitRemoved: %m");
207
208 r = sd_bus_match_signal_async(
209 m->bus,
210 NULL,
211 "org.freedesktop.systemd1",
212 NULL,
213 "org.freedesktop.DBus.Properties",
214 "PropertiesChanged",
215 match_properties_changed, NULL, m);
f647962d 216 if (r < 0)
75152a4d
LP
217 return log_error_errno(r, "Failed to request match for PropertiesChanged: %m");
218
14456f76 219 r = bus_match_signal_async(m->bus, NULL, bus_systemd_mgr, "Reloading", match_reloading, NULL, m);
f647962d 220 if (r < 0)
75152a4d 221 return log_error_errno(r, "Failed to request match for Reloading: %m");
6797c324 222
14456f76 223 r = bus_call_method_async(m->bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL);
31b2cd5d
LP
224 if (r < 0)
225 return log_error_errno(r, "Failed to enable subscription: %m");
1ee306e1 226
ac9f55ed
LP
227 r = bus_log_control_api_register(m->bus);
228 if (r < 0)
229 return r;
230
0c0b9306 231 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.machine1", 0, NULL, NULL);
f647962d 232 if (r < 0)
0c0b9306 233 return log_error_errno(r, "Failed to request name: %m");
1ee306e1 234
c3350683 235 r = sd_bus_attach_event(m->bus, m->event, 0);
f647962d
MS
236 if (r < 0)
237 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1ee306e1 238
1ee306e1 239 return 0;
1ee306e1
LP
240}
241
730fa7ce 242static void manager_gc(Manager *m, bool drop_not_started) {
1ee306e1
LP
243 Machine *machine;
244
245 assert(m);
246
247 while ((machine = m->machine_gc_queue)) {
71fda00f 248 LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
1ee306e1
LP
249 machine->in_gc_queue = false;
250
49f3fffd 251 /* First, if we are not closing yet, initiate stopping */
554ce41f 252 if (machine_may_gc(machine, drop_not_started) &&
49f3fffd 253 machine_get_state(machine) != MACHINE_CLOSING)
1ee306e1 254 machine_stop(machine);
49f3fffd 255
61233823 256 /* Now, the stop probably made this referenced
49f3fffd
LP
257 * again, but if it didn't, then it's time to let it
258 * go entirely. */
554ce41f 259 if (machine_may_gc(machine, drop_not_started)) {
49f3fffd 260 machine_finalize(machine);
1ee306e1
LP
261 machine_free(machine);
262 }
263 }
264}
265
730fa7ce 266static int manager_startup(Manager *m) {
1ee306e1 267 Machine *machine;
c3350683 268 int r;
1ee306e1
LP
269
270 assert(m);
1ee306e1
LP
271
272 /* Connect to the bus */
273 r = manager_connect_bus(m);
274 if (r < 0)
275 return r;
276
4751364e
LP
277 /* Set up Varlink service */
278 r = manager_varlink_init(m);
279 if (r < 0)
280 return r;
281
1ee306e1
LP
282 /* Deserialize state */
283 manager_enumerate_machines(m);
284
285 /* Remove stale objects before we start them */
286 manager_gc(m, false);
287
288 /* And start everything */
90e74a66 289 HASHMAP_FOREACH(machine, m->machines)
c3350683 290 machine_start(machine, NULL, NULL);
1ee306e1
LP
291
292 return 0;
293}
294
d9e34bfd
LP
295static bool check_idle(void *userdata) {
296 Manager *m = userdata;
1ee306e1 297
56599585
LP
298 if (m->operations)
299 return false;
300
4751364e
LP
301 if (varlink_server_current_connections(m->varlink_server) > 0)
302 return false;
303
d9e34bfd 304 manager_gc(m, true);
1ee306e1 305
d9e34bfd
LP
306 return hashmap_isempty(m->machines);
307}
1ee306e1 308
730fa7ce 309static int manager_run(Manager *m) {
d9e34bfd 310 assert(m);
1ee306e1 311
d9e34bfd
LP
312 return bus_event_loop_with_idle(
313 m->event,
314 m->bus,
315 "org.freedesktop.machine1",
316 DEFAULT_EXIT_USEC,
317 check_idle, m);
1ee306e1
LP
318}
319
9b58b5ad 320static int run(int argc, char *argv[]) {
730fa7ce 321 _cleanup_(manager_unrefp) Manager *m = NULL;
1ee306e1
LP
322 int r;
323
1ee306e1 324 log_set_facility(LOG_AUTH);
d2acb93d 325 log_setup();
1ee306e1 326
fc021a5b
ZJS
327 r = service_parse_argv("systemd-machined.service",
328 "Manage registrations of local VMs and containers.",
4faa530c
ZJS
329 BUS_IMPLEMENTATIONS(&manager_object,
330 &log_control_object),
fc021a5b
ZJS
331 argc, argv);
332 if (r <= 0)
333 return r;
1ee306e1 334
fc021a5b 335 umask(0022);
1ee306e1 336
730fa7ce
LP
337 /* Always create the directories people can create inotify watches in. Note that some applications might check
338 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
339 * make sure this check stays in. */
340 (void) mkdir_label("/run/systemd/machines", 0755);
1ee306e1 341
730fa7ce 342 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGTERM, SIGINT, -1) >= 0);
0370612e 343
730fa7ce 344 r = manager_new(&m);
9b58b5ad
ZJS
345 if (r < 0)
346 return log_error_errno(r, "Failed to allocate manager object: %m");
1ee306e1
LP
347
348 r = manager_startup(m);
9b58b5ad
ZJS
349 if (r < 0)
350 return log_error_errno(r, "Failed to fully start up daemon: %m");
1ee306e1 351
df0ff127 352 log_debug("systemd-machined running as pid "PID_FMT, getpid_cached());
4bf4f50f
ZJS
353 r = sd_notify(false, NOTIFY_READY);
354 if (r < 0)
355 log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
1ee306e1
LP
356
357 r = manager_run(m);
358
4bf4f50f 359 (void) sd_notify(false, NOTIFY_STOPPING);
df0ff127 360 log_debug("systemd-machined stopped as pid "PID_FMT, getpid_cached());
9b58b5ad 361 return r;
1ee306e1 362}
9b58b5ad
ZJS
363
364DEFINE_MAIN_FUNCTION(run);