]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined.c
Merge pull request #8689 from davide125/static
[thirdparty/systemd.git] / src / machine / machined.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include "sd-daemon.h"
13
14 #include "alloc-util.h"
15 #include "bus-error.h"
16 #include "bus-util.h"
17 #include "cgroup-util.h"
18 #include "dirent-util.h"
19 #include "fd-util.h"
20 #include "format-util.h"
21 #include "hostname-util.h"
22 #include "label.h"
23 #include "machine-image.h"
24 #include "machined.h"
25 #include "process-util.h"
26 #include "signal-util.h"
27 #include "special.h"
28
29 static Manager* manager_unref(Manager *m);
30 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
31
32 static int manager_new(Manager **ret) {
33 _cleanup_(manager_unrefp) Manager *m = NULL;
34 int r;
35
36 assert(ret);
37
38 m = new0(Manager, 1);
39 if (!m)
40 return -ENOMEM;
41
42 m->machines = hashmap_new(&string_hash_ops);
43 m->machine_units = hashmap_new(&string_hash_ops);
44 m->machine_leaders = hashmap_new(NULL);
45
46 if (!m->machines || !m->machine_units || !m->machine_leaders)
47 return -ENOMEM;
48
49 r = sd_event_default(&m->event);
50 if (r < 0)
51 return r;
52
53 r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
54 if (r < 0)
55 return r;
56
57 r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
58 if (r < 0)
59 return r;
60
61 (void) sd_event_set_watchdog(m->event, true);
62
63 *ret = TAKE_PTR(m);
64 return 0;
65 }
66
67 static Manager* manager_unref(Manager *m) {
68 Machine *machine;
69
70 if (!m)
71 return NULL;
72
73 while (m->operations)
74 operation_free(m->operations);
75
76 assert(m->n_operations == 0);
77
78 while ((machine = hashmap_first(m->machines)))
79 machine_free(machine);
80
81 hashmap_free(m->machines);
82 hashmap_free(m->machine_units);
83 hashmap_free(m->machine_leaders);
84
85 hashmap_free_with_destructor(m->image_cache, image_unref);
86
87 sd_event_source_unref(m->image_cache_defer_event);
88
89 bus_verify_polkit_async_registry_free(m->polkit_registry);
90
91 sd_bus_unref(m->bus);
92 sd_event_unref(m->event);
93
94 return mfree(m);
95 }
96
97 static int manager_add_host_machine(Manager *m) {
98 _cleanup_free_ char *rd = NULL, *unit = NULL;
99 sd_id128_t mid;
100 Machine *t;
101 int r;
102
103 if (m->host_machine)
104 return 0;
105
106 r = sd_id128_get_machine(&mid);
107 if (r < 0)
108 return log_error_errno(r, "Failed to get machine ID: %m");
109
110 rd = strdup("/");
111 if (!rd)
112 return log_oom();
113
114 unit = strdup(SPECIAL_ROOT_SLICE);
115 if (!unit)
116 return log_oom();
117
118 t = machine_new(m, MACHINE_HOST, ".host");
119 if (!t)
120 return log_oom();
121
122 t->leader = 1;
123 t->id = mid;
124
125 t->root_directory = TAKE_PTR(rd);
126 t->unit = TAKE_PTR(unit);
127
128 dual_timestamp_from_boottime_or_monotonic(&t->timestamp, 0);
129
130 m->host_machine = t;
131
132 return 0;
133 }
134
135 static int manager_enumerate_machines(Manager *m) {
136 _cleanup_closedir_ DIR *d = NULL;
137 struct dirent *de;
138 int r = 0;
139
140 assert(m);
141
142 r = manager_add_host_machine(m);
143 if (r < 0)
144 return r;
145
146 /* Read in machine data stored on disk */
147 d = opendir("/run/systemd/machines");
148 if (!d) {
149 if (errno == ENOENT)
150 return 0;
151
152 return log_error_errno(errno, "Failed to open /run/systemd/machines: %m");
153 }
154
155 FOREACH_DIRENT(de, d, return -errno) {
156 struct Machine *machine;
157 int k;
158
159 if (!dirent_is_file(de))
160 continue;
161
162 /* Ignore symlinks that map the unit name to the machine */
163 if (startswith(de->d_name, "unit:"))
164 continue;
165
166 if (!machine_name_is_valid(de->d_name))
167 continue;
168
169 k = manager_add_machine(m, de->d_name, &machine);
170 if (k < 0) {
171 r = log_error_errno(k, "Failed to add machine by file name %s: %m", de->d_name);
172 continue;
173 }
174
175 machine_add_to_gc_queue(machine);
176
177 k = machine_load(machine);
178 if (k < 0)
179 r = k;
180 }
181
182 return r;
183 }
184
185 static int manager_connect_bus(Manager *m) {
186 int r;
187
188 assert(m);
189 assert(!m->bus);
190
191 r = sd_bus_default_system(&m->bus);
192 if (r < 0)
193 return log_error_errno(r, "Failed to connect to system bus: %m");
194
195 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", manager_vtable, m);
196 if (r < 0)
197 return log_error_errno(r, "Failed to add manager object vtable: %m");
198
199 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/machine", "org.freedesktop.machine1.Machine", machine_vtable, machine_object_find, m);
200 if (r < 0)
201 return log_error_errno(r, "Failed to add machine object vtable: %m");
202
203 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/machine", machine_node_enumerator, m);
204 if (r < 0)
205 return log_error_errno(r, "Failed to add machine enumerator: %m");
206
207 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable, image_object_find, m);
208 if (r < 0)
209 return log_error_errno(r, "Failed to add image object vtable: %m");
210
211 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m);
212 if (r < 0)
213 return log_error_errno(r, "Failed to add image enumerator: %m");
214
215 r = sd_bus_match_signal_async(
216 m->bus,
217 NULL,
218 "org.freedesktop.systemd1",
219 "/org/freedesktop/systemd1",
220 "org.freedesktop.systemd1.Manager",
221 "JobRemoved",
222 match_job_removed, NULL, m);
223 if (r < 0)
224 return log_error_errno(r, "Failed to add match for JobRemoved: %m");
225
226 r = sd_bus_match_signal_async(
227 m->bus,
228 NULL,
229 "org.freedesktop.systemd1",
230 "/org/freedesktop/systemd1",
231 "org.freedesktop.systemd1.Manager",
232 "UnitRemoved",
233 match_unit_removed, NULL, m);
234 if (r < 0)
235 return log_error_errno(r, "Failed to request match for UnitRemoved: %m");
236
237 r = sd_bus_match_signal_async(
238 m->bus,
239 NULL,
240 "org.freedesktop.systemd1",
241 NULL,
242 "org.freedesktop.DBus.Properties",
243 "PropertiesChanged",
244 match_properties_changed, NULL, m);
245 if (r < 0)
246 return log_error_errno(r, "Failed to request match for PropertiesChanged: %m");
247
248 r = sd_bus_match_signal_async(
249 m->bus,
250 NULL,
251 "org.freedesktop.systemd1",
252 "/org/freedesktop/systemd1",
253 "org.freedesktop.systemd1.Manager",
254 "Reloading",
255 match_reloading, NULL, m);
256 if (r < 0)
257 return log_error_errno(r, "Failed to request match for Reloading: %m");
258
259 r = sd_bus_call_method_async(
260 m->bus,
261 NULL,
262 "org.freedesktop.systemd1",
263 "/org/freedesktop/systemd1",
264 "org.freedesktop.systemd1.Manager",
265 "Subscribe",
266 NULL, NULL,
267 NULL);
268 if (r < 0)
269 return log_error_errno(r, "Failed to enable subscription: %m");
270
271 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.machine1", 0, NULL, NULL);
272 if (r < 0)
273 return log_error_errno(r, "Failed to request name: %m");
274
275 r = sd_bus_attach_event(m->bus, m->event, 0);
276 if (r < 0)
277 return log_error_errno(r, "Failed to attach bus to event loop: %m");
278
279 return 0;
280 }
281
282 static void manager_gc(Manager *m, bool drop_not_started) {
283 Machine *machine;
284
285 assert(m);
286
287 while ((machine = m->machine_gc_queue)) {
288 LIST_REMOVE(gc_queue, m->machine_gc_queue, machine);
289 machine->in_gc_queue = false;
290
291 /* First, if we are not closing yet, initiate stopping */
292 if (machine_may_gc(machine, drop_not_started) &&
293 machine_get_state(machine) != MACHINE_CLOSING)
294 machine_stop(machine);
295
296 /* Now, the stop probably made this referenced
297 * again, but if it didn't, then it's time to let it
298 * go entirely. */
299 if (machine_may_gc(machine, drop_not_started)) {
300 machine_finalize(machine);
301 machine_free(machine);
302 }
303 }
304 }
305
306 static int manager_startup(Manager *m) {
307 Machine *machine;
308 Iterator i;
309 int r;
310
311 assert(m);
312
313 /* Connect to the bus */
314 r = manager_connect_bus(m);
315 if (r < 0)
316 return r;
317
318 /* Deserialize state */
319 manager_enumerate_machines(m);
320
321 /* Remove stale objects before we start them */
322 manager_gc(m, false);
323
324 /* And start everything */
325 HASHMAP_FOREACH(machine, m->machines, i)
326 machine_start(machine, NULL, NULL);
327
328 return 0;
329 }
330
331 static bool check_idle(void *userdata) {
332 Manager *m = userdata;
333
334 if (m->operations)
335 return false;
336
337 manager_gc(m, true);
338
339 return hashmap_isempty(m->machines);
340 }
341
342 static int manager_run(Manager *m) {
343 assert(m);
344
345 return bus_event_loop_with_idle(
346 m->event,
347 m->bus,
348 "org.freedesktop.machine1",
349 DEFAULT_EXIT_USEC,
350 check_idle, m);
351 }
352
353 int main(int argc, char *argv[]) {
354 _cleanup_(manager_unrefp) Manager *m = NULL;
355 int r;
356
357 log_set_target(LOG_TARGET_AUTO);
358 log_set_facility(LOG_AUTH);
359 log_parse_environment();
360 log_open();
361
362 umask(0022);
363
364 if (argc != 1) {
365 log_error("This program takes no arguments.");
366 r = -EINVAL;
367 goto finish;
368 }
369
370 /* Always create the directories people can create inotify watches in. Note that some applications might check
371 * for the existence of /run/systemd/machines/ to determine whether machined is available, so please always
372 * make sure this check stays in. */
373 (void) mkdir_label("/run/systemd/machines", 0755);
374
375 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGTERM, SIGINT, -1) >= 0);
376
377 r = manager_new(&m);
378 if (r < 0) {
379 log_error_errno(r, "Failed to allocate manager object: %m");
380 goto finish;
381 }
382
383 r = manager_startup(m);
384 if (r < 0) {
385 log_error_errno(r, "Failed to fully start up daemon: %m");
386 goto finish;
387 }
388
389 log_debug("systemd-machined running as pid "PID_FMT, getpid_cached());
390
391 (void) sd_notify(false,
392 "READY=1\n"
393 "STATUS=Processing requests...");
394
395 r = manager_run(m);
396
397 log_debug("systemd-machined stopped as pid "PID_FMT, getpid_cached());
398
399 (void) sd_notify(false,
400 "STOPPING=1\n"
401 "STATUS=Shutting down...");
402
403 finish:
404 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
405 }