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