Debian Codegraph @

src/machine/machined.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <string.h>
4 #include <sys/stat.h>
5
6 #include "sd-bus.h"
7 #include "sd-event.h"
8 #include "sd-varlink.h"
9
10 #include "alloc-util.h"
11 #include "bus-locator.h"
12 #include "bus-log-control-api.h"
13 #include "bus-object.h"
14 #include "bus-util.h"
15 #include "common-signal.h"
16 #include "constants.h"
17 #include "daemon-util.h"
18 #include "dirent-util.h"
19 #include "errno-util.h"
20 #include "fd-util.h"
21 #include "hash-funcs.h"
22 #include "hashmap.h"
23 #include "hostname-util.h"
24 #include "machine.h"
25 #include "machined.h"
26 #include "machined-dbus.h"
27 #include "machined-varlink.h"
28 #include "main-func.h"
29 #include "mkdir.h"
30 #include "operation.h"
31 #include "path-lookup.h"
32 #include "service-util.h"
33 #include "set.h"
34 #include "signal-util.h"
35 #include "special.h"
36 #include "string-util.h"
37 #include "vsock-util.h"
38
39 static Manager* manager_unref(Manager *m);
40 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
41
42 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops, char, string_hash_func, string_compare_func, Machine, machine_free);
43
44 static int manager_new(RuntimeScope scope, Manager **ret) {
45 _cleanup_(manager_unrefp) Manager *m = NULL;
46 int r;
47
48 assert(ret);
49
50 m = new(Manager, 1);
51 if (!m)
52 return -ENOMEM;
53
54 *m = (Manager) {
55 .runtime_scope = scope,
56 };
57
58 r = runtime_directory_generic(scope, "systemd/machines", &m->state_dir);
59 if (r < 0)
60 return r;
61
62 m->machines = hashmap_new(&machine_hash_ops);
63 if (!m->machines)
64 return -ENOMEM;
65
66 r = sd_event_default(&m->event);
67 if (r < 0)
68 return r;
69
70 r = sd_event_set_signal_exit(m->event, true);
71 if (r < 0)
72 return r;
73
74 r = sd_event_add_signal(m->event, /* ret= */ NULL, (SIGRTMIN+18)|SD_EVENT_SIGNAL_PROCMASK, sigrtmin18_handler, /* userdata= */ NULL);
75 if (r < 0)
76 return r;
77
78 r = sd_event_add_memory_pressure(m->event, NULL, NULL, NULL);
79 if (r < 0)
80 log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || r == -EHOSTDOWN ? LOG_DEBUG : LOG_NOTICE, r,
81 "Unable to create memory pressure event source, ignoring: %m");
82
83 (void) sd_event_set_watchdog(m->event, true);
84
85 *ret = TAKE_PTR(m);
86 return 0;
87 }
88
89 static Manager* manager_unref(Manager *m) {
90 if (!m)
91 return NULL;
92
93 while (m->operations)
94 operation_free(m->operations);
95
96 assert(m->n_operations == 0);
97
98 hashmap_free(m->machines); /* This will free all machines, thus the by_unit/by_leader hashmaps shall be empty */
99
100 assert(hashmap_isempty(m->machines_by_unit));
101 hashmap_free(m->machines_by_unit);
102 assert(hashmap_isempty(m->machines_by_leader));
103 hashmap_free(m->machines_by_leader);
104
105 hashmap_free(m->image_cache);
106
107 sd_event_source_unref(m->image_cache_defer_event);
108
109 sd_event_source_disable_unref(m->deferred_gc_event_source);
110
111 hashmap_free(m->polkit_registry);
112
113 manager_varlink_done(m);
114
115 m->query_filter_subscriptions = set_free(m->query_filter_subscriptions);
116
117 sd_bus_flush_close_unref(m->api_bus);
118 sd_bus_flush_close_unref(m->system_bus);
119 sd_event_unref(m->event);
120
121 free(m->state_dir);
122
123 return mfree(m);
124 }
125
126 static int manager_add_host_machine(Manager *m) {
127 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
128 _cleanup_free_ char *rd = NULL, *unit = NULL;
129 sd_id128_t mid;
130 Machine *t;
131 int r;
132
133 if (m->runtime_scope != RUNTIME_SCOPE_SYSTEM)
134 return 0;
135 if (m->host_machine)
136 return 0;
137
138 r = sd_id128_get_machine(&mid);
139 if (r < 0)
140 return log_error_errno(r, "Failed to get machine ID: %m");
141
142 rd = strdup("/");
143 if (!rd)
144 return log_oom();
145
146 unit = strdup(SPECIAL_ROOT_SLICE);
147 if (!unit)
148 return log_oom();
149
150 r = pidref_set_pid(&pidref, 1);
151 if (r < 0)
152 return log_error_errno(r, "Failed to open reference to PID 1: %m");
153
154 r = machine_new(MACHINE_HOST, ".host", &t);
155 if (r < 0)
156 return log_error_errno(r, "Failed to create machine: %m");
157
158 r = machine_link(m, t);
159 if (r < 0)
160 return log_error_errno(r, "Failed to link machine to manager: %m");
161
162 t->leader = TAKE_PIDREF(pidref);
163 t->id = mid;
164
165 /* If vsock is available, let's expose the loopback CID for the local host (and not the actual local
166 * CID, in order to return a ideally constant record for the host) */
167 if (vsock_get_local_cid(/* ret= */ NULL) >= 0)
168 t->vsock_cid = VMADDR_CID_LOCAL;
169
170 t->root_directory = TAKE_PTR(rd);
171 t->unit = TAKE_PTR(unit);
172
173 dual_timestamp_from_boottime(&t->timestamp, 0);
174
175 m->host_machine = t;
176
177 return 0;
178 }
179
180 static int manager_enumerate_machines(Manager *m) {
181 _cleanup_closedir_ DIR *d = NULL;
182 int r;
183
184 assert(m);
185
186 r = manager_add_host_machine(m);
187 if (r < 0)
188 return r;
189
190 /* Read in machine data stored on disk */
191 d = opendir(m->state_dir);
192 if (!d) {
193 if (errno == ENOENT)
194 return 0;
195
196 return log_error_errno(errno, "Failed to open '%s': %m", m->state_dir);
197 }
198
199 FOREACH_DIRENT(de, d, return -errno) {
200 struct Machine *machine;
201 int k;
202
203 if (!dirent_is_file(de))
204 continue;
205
206 /* Ignore symlinks that map the unit name to the machine */
207 if (startswith(de->d_name, "unit:"))
208 continue;
209
210 if (!hostname_is_valid(de->d_name, 0))
211 continue;
212
213 k = manager_add_machine(m, de->d_name, &machine);
214 if (k < 0) {
215 RET_GATHER(r, log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name));
216 continue;
217 }
218
219 machine_add_to_gc_queue(machine);
220
221 RET_GATHER(r, machine_load(machine));
222 }
223
224 return r;
225 }
226
227 static int manager_connect_bus(Manager *m) {
228 int r;
229
230 assert(m);
231 assert(!m->system_bus);
232 assert(!m->api_bus);
233
234 r = sd_bus_default_system(&m->system_bus);
235 if (r < 0)
236 return log_error_errno(r, "Failed to connect to system bus: %m");
237
238 r = sd_bus_attach_event(m->system_bus, m->event, 0);
239 if (r < 0)
240 return log_error_errno(r, "Failed to attach system bus to event loop: %m");
241
242 if (m->runtime_scope == RUNTIME_SCOPE_SYSTEM)
243 m->api_bus = sd_bus_ref(m->system_bus);
244 else {
245 assert(m->runtime_scope == RUNTIME_SCOPE_USER);
246
247 r = sd_bus_default_user(&m->api_bus);
248 if (r < 0)
249 return log_error_errno(r, "Failed to connect to user bus: %m");
250
251 r = sd_bus_attach_event(m->api_bus, m->event, 0);
252 if (r < 0)
253 return log_error_errno(r, "Failed to attach user bus to event loop: %m");
254 }
255
256 r = bus_add_implementation(m->api_bus, &manager_object, m);
257 if (r < 0)
258 return r;
259
260 r = bus_match_signal_async(m->api_bus, NULL, bus_systemd_mgr, "JobRemoved", match_job_removed, NULL, m);
261 if (r < 0)
262 return log_error_errno(r, "Failed to add match for JobRemoved: %m");
263
264 r = bus_match_signal_async(m->api_bus, NULL, bus_systemd_mgr, "UnitRemoved", match_unit_removed, NULL, m);
265 if (r < 0)
266 return log_error_errno(r, "Failed to request match for UnitRemoved: %m");
267
268 r = sd_bus_match_signal_async(
269 m->api_bus,
270 NULL,
271 "org.freedesktop.systemd1",
272 NULL,
273 "org.freedesktop.DBus.Properties",
274 "PropertiesChanged",
275 match_properties_changed, NULL, m);
276 if (r < 0)
277 return log_error_errno(r, "Failed to request match for PropertiesChanged: %m");
278
279 r = bus_match_signal_async(m->api_bus, NULL, bus_systemd_mgr, "Reloading", match_reloading, NULL, m);
280 if (r < 0)
281 return log_error_errno(r, "Failed to request match for Reloading: %m");
282
283 r = bus_call_method_async(m->api_bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL);
284 if (r < 0)
285 return log_error_errno(r, "Failed to enable subscription: %m");
286
287 r = bus_log_control_api_register(m->api_bus);
288 if (r < 0)
289 return r;
290
291 r = sd_bus_request_name_async(m->api_bus, NULL, "org.freedesktop.machine1", 0, NULL, NULL);
292 if (r < 0)
293 return log_error_errno(r, "Failed to request name: %m");
294
295 return 0;
296 }
297
298 static int manager_startup(Manager *m) {
299 Machine *machine;
300 int r;
301
302 assert(m);
303
304 /* Connect to the bus */
305 r = manager_connect_bus(m);
306 if (r < 0)
307 return r;
308
309 /* Set up Varlink service */
310 r = manager_varlink_init(m);
311 if (r < 0)
312 return r;
313
314 /* Deserialize state */
315 manager_enumerate_machines(m);
316
317 /* Remove stale objects before we start them */
318 manager_gc(m, false);
319
320 /* And start everything */
321 HASHMAP_FOREACH(machine, m->machines)
322 machine_start(machine, NULL, NULL);
323
324 return 0;
325 }
326
327 static bool check_idle(void *userdata) {
328 Manager *m = ASSERT_PTR(userdata);
329
330 if (m->operations)
331 return false;
332
333 if (sd_varlink_server_current_connections(m->varlink_userdb_server) > 0)
334 return false;
335
336 if (sd_varlink_server_current_connections(m->varlink_machine_server) > 0)
337 return false;
338
339 if (!hashmap_isempty(m->polkit_registry))
340 return false;
341
342 return hashmap_isempty(m->machines);
343 }
344
345 static int run(int argc, char *argv[]) {
346 _cleanup_(manager_unrefp) Manager *m = NULL;
347 RuntimeScope scope = RUNTIME_SCOPE_SYSTEM;
348 int r;
349
350 log_set_facility(LOG_AUTH);
351 log_setup();
352
353 r = service_parse_argv("systemd-machined.service",
354 "Manage registrations of local VMs and containers.",
355 BUS_IMPLEMENTATIONS(&manager_object,
356 &log_control_object),
357 &scope,
358 argc, argv);
359 if (r <= 0)
360 return r;
361
362 umask(0022);
363
364 /* Always create the directories people can create inotify watches in. Note that some applications might check
365 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
366 * make sure this check stays in. */
367 if (scope == RUNTIME_SCOPE_SYSTEM)
368 (void) mkdir_label("/run/systemd/machines", 0755);
369
370 r = manager_new(scope, &m);
371 if (r < 0)
372 return log_error_errno(r, "Failed to allocate manager object: %m");
373
374 r = manager_startup(m);
375 if (r < 0)
376 return log_error_errno(r, "Failed to fully start up daemon: %m");
377
378 r = sd_notify(false, NOTIFY_READY_MESSAGE);
379 if (r < 0)
380 log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
381
382 r = bus_event_loop_with_idle(
383 m->event,
384 m->api_bus,
385 "org.freedesktop.machine1",
386 DEFAULT_EXIT_USEC,
387 check_idle, m);
388 if (r < 0)
389 return log_error_errno(r, "Failed to run main loop: %m");
390
391 return 0;
392 }
393
394 DEFINE_MAIN_FUNCTION(run);