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