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