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