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