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