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