]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
9444b1f2 | 2 | |
66cb2fde | 3 | #include <errno.h> |
9444b1f2 | 4 | #include <unistd.h> |
ca78ad1d | 5 | #include <sys/stat.h> |
9444b1f2 | 6 | |
c3350683 | 7 | #include "sd-messages.h" |
fb6becb4 | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
66cb2fde | 10 | #include "bus-error.h" |
57f28dee | 11 | #include "bus-locator.h" |
66cb2fde | 12 | #include "bus-util.h" |
686d13b9 | 13 | #include "env-file.h" |
66855de7 | 14 | #include "errno-util.h" |
4f5dd394 | 15 | #include "escape.h" |
cf0fbc49 | 16 | #include "extract-word.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
9444b1f2 | 18 | #include "fileio.h" |
f97b34a6 | 19 | #include "format-util.h" |
66cb2fde | 20 | #include "hashmap.h" |
4f5dd394 | 21 | #include "machine-dbus.h" |
3ffd4af2 | 22 | #include "machine.h" |
35cd0ba5 | 23 | #include "mkdir-label.h" |
6bedfcbb | 24 | #include "parse-util.h" |
b910cc72 | 25 | #include "path-util.h" |
4a0b58c4 | 26 | #include "process-util.h" |
d68c645b | 27 | #include "serialize.h" |
9444b1f2 | 28 | #include "special.h" |
3401419b | 29 | #include "stdio-util.h" |
8b43440b | 30 | #include "string-table.h" |
66cb2fde | 31 | #include "terminal-util.h" |
e4de7287 | 32 | #include "tmpfile-util.h" |
fb6becb4 | 33 | #include "unit-name.h" |
3a664727 | 34 | #include "user-util.h" |
9444b1f2 | 35 | |
fbe55073 | 36 | Machine* machine_new(Manager *manager, MachineClass class, const char *name) { |
9444b1f2 LP |
37 | Machine *m; |
38 | ||
39 | assert(manager); | |
fbe55073 | 40 | assert(class < _MACHINE_CLASS_MAX); |
9444b1f2 LP |
41 | assert(name); |
42 | ||
fbe55073 LP |
43 | /* Passing class == _MACHINE_CLASS_INVALID here is fine. It |
44 | * means as much as "we don't know yet", and that we'll figure | |
45 | * it out later when loading the state file. */ | |
46 | ||
9444b1f2 LP |
47 | m = new0(Machine, 1); |
48 | if (!m) | |
49 | return NULL; | |
50 | ||
51 | m->name = strdup(name); | |
52 | if (!m->name) | |
53 | goto fail; | |
54 | ||
fbe55073 | 55 | if (class != MACHINE_HOST) { |
b910cc72 | 56 | m->state_file = path_join("/run/systemd/machines", m->name); |
fbe55073 LP |
57 | if (!m->state_file) |
58 | goto fail; | |
59 | } | |
60 | ||
61 | m->class = class; | |
9444b1f2 LP |
62 | |
63 | if (hashmap_put(manager->machines, m->name, m) < 0) | |
64 | goto fail; | |
65 | ||
9444b1f2 LP |
66 | m->manager = manager; |
67 | ||
68 | return m; | |
69 | ||
70 | fail: | |
71 | free(m->state_file); | |
72 | free(m->name); | |
6b430fdb | 73 | return mfree(m); |
9444b1f2 LP |
74 | } |
75 | ||
bb1a05d6 YW |
76 | Machine* machine_free(Machine *m) { |
77 | if (!m) | |
78 | return NULL; | |
9444b1f2 | 79 | |
0370612e | 80 | while (m->operations) |
795c5d31 | 81 | operation_free(m->operations); |
0370612e | 82 | |
9444b1f2 | 83 | if (m->in_gc_queue) |
71fda00f | 84 | LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 | 85 | |
9b420b3c | 86 | machine_release_unit(m); |
9444b1f2 | 87 | |
fb6becb4 LP |
88 | free(m->scope_job); |
89 | ||
9b420b3c | 90 | (void) hashmap_remove(m->manager->machines, m->name); |
9444b1f2 | 91 | |
fbe55073 LP |
92 | if (m->manager->host_machine == m) |
93 | m->manager->host_machine = NULL; | |
94 | ||
d3e84ddb | 95 | if (m->leader > 0) |
4a0b58c4 | 96 | (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb | 97 | |
c3350683 | 98 | sd_bus_message_unref(m->create_message); |
fb6becb4 | 99 | |
9444b1f2 LP |
100 | free(m->name); |
101 | free(m->state_file); | |
102 | free(m->service); | |
9444b1f2 | 103 | free(m->root_directory); |
9b5ed6fe | 104 | free(m->netif); |
bb1a05d6 | 105 | return mfree(m); |
9444b1f2 LP |
106 | } |
107 | ||
108 | int machine_save(Machine *m) { | |
109 | _cleanup_free_ char *temp_path = NULL; | |
110 | _cleanup_fclose_ FILE *f = NULL; | |
111 | int r; | |
112 | ||
113 | assert(m); | |
fbe55073 LP |
114 | |
115 | if (!m->state_file) | |
116 | return 0; | |
9444b1f2 LP |
117 | |
118 | if (!m->started) | |
119 | return 0; | |
120 | ||
37c1d5e9 | 121 | r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE); |
9444b1f2 | 122 | if (r < 0) |
dacd6cee | 123 | goto fail; |
9444b1f2 LP |
124 | |
125 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
126 | if (r < 0) | |
dacd6cee | 127 | goto fail; |
9444b1f2 | 128 | |
dacd6cee | 129 | (void) fchmod(fileno(f), 0644); |
9444b1f2 LP |
130 | |
131 | fprintf(f, | |
132 | "# This is private data. Do not parse.\n" | |
133 | "NAME=%s\n", | |
134 | m->name); | |
135 | ||
ca5405bb | 136 | if (m->unit) { |
c2b2df60 | 137 | _cleanup_free_ char *escaped = NULL; |
ca5405bb LP |
138 | |
139 | escaped = cescape(m->unit); | |
140 | if (!escaped) { | |
141 | r = -ENOMEM; | |
dacd6cee | 142 | goto fail; |
ca5405bb LP |
143 | } |
144 | ||
145 | fprintf(f, "SCOPE=%s\n", escaped); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */ | |
146 | } | |
fb6becb4 LP |
147 | |
148 | if (m->scope_job) | |
149 | fprintf(f, "SCOPE_JOB=%s\n", m->scope_job); | |
9444b1f2 | 150 | |
ca5405bb | 151 | if (m->service) { |
c2b2df60 | 152 | _cleanup_free_ char *escaped = NULL; |
9444b1f2 | 153 | |
ca5405bb LP |
154 | escaped = cescape(m->service); |
155 | if (!escaped) { | |
156 | r = -ENOMEM; | |
dacd6cee | 157 | goto fail; |
ca5405bb LP |
158 | } |
159 | fprintf(f, "SERVICE=%s\n", escaped); | |
160 | } | |
161 | ||
162 | if (m->root_directory) { | |
c2b2df60 | 163 | _cleanup_free_ char *escaped = NULL; |
ca5405bb LP |
164 | |
165 | escaped = cescape(m->root_directory); | |
166 | if (!escaped) { | |
167 | r = -ENOMEM; | |
dacd6cee | 168 | goto fail; |
ca5405bb LP |
169 | } |
170 | fprintf(f, "ROOT=%s\n", escaped); | |
171 | } | |
9444b1f2 | 172 | |
3bbaff3e | 173 | if (!sd_id128_is_null(m->id)) |
9444b1f2 LP |
174 | fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id)); |
175 | ||
176 | if (m->leader != 0) | |
90b2de37 | 177 | fprintf(f, "LEADER="PID_FMT"\n", m->leader); |
9444b1f2 LP |
178 | |
179 | if (m->class != _MACHINE_CLASS_INVALID) | |
180 | fprintf(f, "CLASS=%s\n", machine_class_to_string(m->class)); | |
181 | ||
182 | if (dual_timestamp_is_set(&m->timestamp)) | |
183 | fprintf(f, | |
90b2de37 ZJS |
184 | "REALTIME="USEC_FMT"\n" |
185 | "MONOTONIC="USEC_FMT"\n", | |
186 | m->timestamp.realtime, | |
187 | m->timestamp.monotonic); | |
9444b1f2 | 188 | |
9b5ed6fe | 189 | if (m->n_netif > 0) { |
68e16e9c | 190 | size_t i; |
9b5ed6fe | 191 | |
0d536673 | 192 | fputs("NETIF=", f); |
9b5ed6fe LP |
193 | |
194 | for (i = 0; i < m->n_netif; i++) { | |
195 | if (i != 0) | |
0d536673 | 196 | fputc(' ', f); |
9b5ed6fe LP |
197 | |
198 | fprintf(f, "%i", m->netif[i]); | |
199 | } | |
200 | ||
0d536673 | 201 | fputc('\n', f); |
9b5ed6fe LP |
202 | } |
203 | ||
034753ac LP |
204 | r = fflush_and_check(f); |
205 | if (r < 0) | |
dacd6cee | 206 | goto fail; |
9444b1f2 | 207 | |
034753ac | 208 | if (rename(temp_path, m->state_file) < 0) { |
9444b1f2 | 209 | r = -errno; |
dacd6cee | 210 | goto fail; |
9444b1f2 LP |
211 | } |
212 | ||
89f7c846 LP |
213 | if (m->unit) { |
214 | char *sl; | |
215 | ||
216 | /* Create a symlink from the unit name to the machine | |
217 | * name, so that we can quickly find the machine for | |
e62d9b81 | 218 | * each given unit. Ignore error. */ |
63c372cb | 219 | sl = strjoina("/run/systemd/machines/unit:", m->unit); |
e62d9b81 | 220 | (void) symlink(m->name, sl); |
89f7c846 LP |
221 | } |
222 | ||
dacd6cee | 223 | return 0; |
034753ac | 224 | |
dacd6cee LP |
225 | fail: |
226 | (void) unlink(m->state_file); | |
227 | ||
228 | if (temp_path) | |
229 | (void) unlink(temp_path); | |
9444b1f2 | 230 | |
dacd6cee | 231 | return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file); |
9444b1f2 LP |
232 | } |
233 | ||
89f7c846 LP |
234 | static void machine_unlink(Machine *m) { |
235 | assert(m); | |
236 | ||
237 | if (m->unit) { | |
89f7c846 LP |
238 | char *sl; |
239 | ||
63c372cb | 240 | sl = strjoina("/run/systemd/machines/unit:", m->unit); |
491ac9f2 | 241 | (void) unlink(sl); |
89f7c846 LP |
242 | } |
243 | ||
244 | if (m->state_file) | |
491ac9f2 | 245 | (void) unlink(m->state_file); |
89f7c846 LP |
246 | } |
247 | ||
9444b1f2 | 248 | int machine_load(Machine *m) { |
9b5ed6fe | 249 | _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL; |
9444b1f2 LP |
250 | int r; |
251 | ||
252 | assert(m); | |
253 | ||
fbe55073 LP |
254 | if (!m->state_file) |
255 | return 0; | |
256 | ||
aa8fbc74 | 257 | r = parse_env_file(NULL, m->state_file, |
89f7c846 | 258 | "SCOPE", &m->unit, |
fb6becb4 | 259 | "SCOPE_JOB", &m->scope_job, |
9444b1f2 | 260 | "SERVICE", &m->service, |
9444b1f2 LP |
261 | "ROOT", &m->root_directory, |
262 | "ID", &id, | |
263 | "LEADER", &leader, | |
264 | "CLASS", &class, | |
265 | "REALTIME", &realtime, | |
266 | "MONOTONIC", &monotonic, | |
13df9c39 | 267 | "NETIF", &netif); |
40f35786 ZJS |
268 | if (r == -ENOENT) |
269 | return 0; | |
270 | if (r < 0) | |
8d3d7072 | 271 | return log_error_errno(r, "Failed to read %s: %m", m->state_file); |
9444b1f2 LP |
272 | |
273 | if (id) | |
274 | sd_id128_from_string(id, &m->id); | |
275 | ||
276 | if (leader) | |
277 | parse_pid(leader, &m->leader); | |
278 | ||
279 | if (class) { | |
280 | MachineClass c; | |
281 | ||
282 | c = machine_class_from_string(class); | |
283 | if (c >= 0) | |
284 | m->class = c; | |
285 | } | |
286 | ||
b895a735 | 287 | if (realtime) |
d68c645b | 288 | (void) deserialize_usec(realtime, &m->timestamp.realtime); |
b895a735 | 289 | if (monotonic) |
d68c645b | 290 | (void) deserialize_usec(monotonic, &m->timestamp.monotonic); |
9444b1f2 | 291 | |
9b5ed6fe | 292 | if (netif) { |
597da51b | 293 | _cleanup_free_ int *ni = NULL; |
319a4f4b LP |
294 | size_t nr = 0; |
295 | const char *p; | |
9b5ed6fe | 296 | |
75a8fd6a | 297 | p = netif; |
9ed794a3 | 298 | for (;;) { |
75a8fd6a | 299 | _cleanup_free_ char *word = NULL; |
9b5ed6fe | 300 | |
75a8fd6a | 301 | r = extract_first_word(&p, &word, NULL, 0); |
75a8fd6a SS |
302 | if (r == 0) |
303 | break; | |
6a37c684 | 304 | if (r == -ENOMEM) |
52278ad3 | 305 | return log_oom(); |
6a37c684 | 306 | if (r < 0) { |
52278ad3 | 307 | log_warning_errno(r, "Failed to parse NETIF: %s", netif); |
6a37c684 | 308 | break; |
52278ad3 | 309 | } |
75a8fd6a | 310 | |
597da51b ZJS |
311 | r = parse_ifindex(word); |
312 | if (r < 0) | |
9b5ed6fe LP |
313 | continue; |
314 | ||
319a4f4b | 315 | if (!GREEDY_REALLOC(ni, nr + 1)) |
9b5ed6fe | 316 | return log_oom(); |
9b5ed6fe | 317 | |
597da51b | 318 | ni[nr++] = r; |
9b5ed6fe LP |
319 | } |
320 | ||
319a4f4b | 321 | free_and_replace(m->netif, ni); |
9b5ed6fe LP |
322 | m->n_netif = nr; |
323 | } | |
324 | ||
9444b1f2 LP |
325 | return r; |
326 | } | |
327 | ||
af227947 | 328 | static int machine_start_scope( |
a01ecfa9 | 329 | Machine *machine, |
af227947 | 330 | sd_bus_message *more_properties, |
a01ecfa9 | 331 | sd_bus_error *error) { |
af227947 ZJS |
332 | |
333 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
a01ecfa9 ZJS |
334 | _cleanup_free_ char *escaped = NULL, *unit = NULL; |
335 | const char *description; | |
af227947 ZJS |
336 | int r; |
337 | ||
a01ecfa9 ZJS |
338 | assert(machine); |
339 | assert(machine->leader > 0); | |
340 | assert(!machine->unit); | |
341 | ||
342 | escaped = unit_name_escape(machine->name); | |
343 | if (!escaped) | |
344 | return log_oom(); | |
345 | ||
346 | unit = strjoin("machine-", escaped, ".scope"); | |
347 | if (!unit) | |
348 | return log_oom(); | |
af227947 | 349 | |
57f28dee | 350 | r = bus_message_new_method_call( |
a01ecfa9 | 351 | machine->manager->bus, |
af227947 | 352 | &m, |
57f28dee | 353 | bus_systemd_mgr, |
af227947 ZJS |
354 | "StartTransientUnit"); |
355 | if (r < 0) | |
356 | return r; | |
357 | ||
a01ecfa9 | 358 | r = sd_bus_message_append(m, "ss", unit, "fail"); |
af227947 ZJS |
359 | if (r < 0) |
360 | return r; | |
361 | ||
362 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
363 | if (r < 0) | |
364 | return r; | |
365 | ||
a01ecfa9 ZJS |
366 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", SPECIAL_MACHINE_SLICE); |
367 | if (r < 0) | |
368 | return r; | |
af227947 | 369 | |
a01ecfa9 ZJS |
370 | description = strjoina(machine->class == MACHINE_VM ? "Virtual Machine " : "Container ", machine->name); |
371 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); | |
372 | if (r < 0) | |
373 | return r; | |
af227947 ZJS |
374 | |
375 | r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)", | |
a01ecfa9 | 376 | "PIDs", "au", 1, machine->leader, |
af227947 ZJS |
377 | "Delegate", "b", 1, |
378 | "CollectMode", "s", "inactive-or-failed", | |
379 | "AddRef", "b", 1, | |
380 | "TasksMax", "t", UINT64_C(16384)); | |
381 | if (r < 0) | |
382 | return r; | |
383 | ||
384 | if (more_properties) { | |
385 | r = sd_bus_message_copy(m, more_properties, true); | |
386 | if (r < 0) | |
387 | return r; | |
388 | } | |
389 | ||
390 | r = sd_bus_message_close_container(m); | |
391 | if (r < 0) | |
392 | return r; | |
393 | ||
394 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
395 | if (r < 0) | |
396 | return r; | |
397 | ||
a01ecfa9 | 398 | r = sd_bus_call(NULL, m, 0, error, &reply); |
af227947 ZJS |
399 | if (r < 0) |
400 | return r; | |
401 | ||
a01ecfa9 ZJS |
402 | machine->unit = TAKE_PTR(unit); |
403 | machine->referenced = true; | |
af227947 | 404 | |
a01ecfa9 ZJS |
405 | const char *job; |
406 | r = sd_bus_message_read(reply, "o", &job); | |
407 | if (r < 0) | |
408 | return r; | |
af227947 | 409 | |
a01ecfa9 | 410 | return free_and_strdup(&machine->scope_job, job); |
af227947 ZJS |
411 | } |
412 | ||
413 | static int machine_ensure_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) { | |
a01ecfa9 ZJS |
414 | int r; |
415 | ||
9444b1f2 | 416 | assert(m); |
fbe55073 | 417 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 418 | |
89f7c846 | 419 | if (!m->unit) { |
a01ecfa9 | 420 | r = machine_start_scope(m, properties, error); |
354f62cf YW |
421 | if (r < 0) |
422 | return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r)); | |
9444b1f2 LP |
423 | } |
424 | ||
a01ecfa9 ZJS |
425 | assert(m->unit); |
426 | hashmap_put(m->manager->machine_units, m->unit, m); | |
d0af76e6 | 427 | |
354f62cf | 428 | return 0; |
9444b1f2 LP |
429 | } |
430 | ||
c3350683 | 431 | int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 LP |
432 | int r; |
433 | ||
434 | assert(m); | |
435 | ||
fbe55073 LP |
436 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
437 | return -EOPNOTSUPP; | |
438 | ||
9444b1f2 LP |
439 | if (m->started) |
440 | return 0; | |
441 | ||
4a0b58c4 | 442 | r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb LP |
443 | if (r < 0) |
444 | return r; | |
445 | ||
fb6becb4 | 446 | /* Create cgroup */ |
af227947 | 447 | r = machine_ensure_scope(m, properties, error); |
fb6becb4 LP |
448 | if (r < 0) |
449 | return r; | |
450 | ||
9444b1f2 | 451 | log_struct(LOG_INFO, |
2b044526 | 452 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR, |
9444b1f2 | 453 | "NAME=%s", m->name, |
de0671ee | 454 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 455 | LOG_MESSAGE("New machine %s.", m->name)); |
9444b1f2 | 456 | |
9444b1f2 LP |
457 | if (!dual_timestamp_is_set(&m->timestamp)) |
458 | dual_timestamp_get(&m->timestamp); | |
459 | ||
460 | m->started = true; | |
461 | ||
462 | /* Save new machine data */ | |
463 | machine_save(m); | |
464 | ||
465 | machine_send_signal(m, true); | |
9fdcbae5 | 466 | (void) manager_enqueue_nscd_cache_flush(m->manager); |
9444b1f2 LP |
467 | |
468 | return 0; | |
469 | } | |
470 | ||
9444b1f2 | 471 | int machine_stop(Machine *m) { |
49f3fffd | 472 | int r; |
69887664 | 473 | |
49f3fffd LP |
474 | assert(m); |
475 | ||
fbe55073 LP |
476 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
477 | return -EOPNOTSUPP; | |
478 | ||
69887664 ZJS |
479 | if (m->unit) { |
480 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
481 | char *job = NULL; | |
482 | ||
483 | r = manager_stop_unit(m->manager, m->unit, &error, &job); | |
484 | if (r < 0) | |
485 | return log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r)); | |
486 | ||
487 | free_and_replace(m->scope_job, job); | |
488 | } | |
49f3fffd LP |
489 | |
490 | m->stopping = true; | |
491 | ||
492 | machine_save(m); | |
9fdcbae5 | 493 | (void) manager_enqueue_nscd_cache_flush(m->manager); |
49f3fffd | 494 | |
69887664 | 495 | return 0; |
49f3fffd LP |
496 | } |
497 | ||
498 | int machine_finalize(Machine *m) { | |
9444b1f2 LP |
499 | assert(m); |
500 | ||
ef8ff92e | 501 | if (m->started) { |
9444b1f2 | 502 | log_struct(LOG_INFO, |
2b044526 | 503 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR, |
9444b1f2 | 504 | "NAME=%s", m->name, |
de0671ee | 505 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 506 | LOG_MESSAGE("Machine %s terminated.", m->name)); |
9444b1f2 | 507 | |
ef8ff92e ZJS |
508 | m->stopping = true; /* The machine is supposed to be going away. Don't try to kill it. */ |
509 | } | |
510 | ||
89f7c846 | 511 | machine_unlink(m); |
9444b1f2 LP |
512 | machine_add_to_gc_queue(m); |
513 | ||
49f3fffd | 514 | if (m->started) { |
9444b1f2 | 515 | machine_send_signal(m, false); |
49f3fffd LP |
516 | m->started = false; |
517 | } | |
9444b1f2 | 518 | |
49f3fffd | 519 | return 0; |
9444b1f2 LP |
520 | } |
521 | ||
554ce41f | 522 | bool machine_may_gc(Machine *m, bool drop_not_started) { |
9444b1f2 LP |
523 | assert(m); |
524 | ||
fbe55073 | 525 | if (m->class == MACHINE_HOST) |
554ce41f | 526 | return false; |
fbe55073 | 527 | |
9444b1f2 | 528 | if (drop_not_started && !m->started) |
554ce41f | 529 | return true; |
9444b1f2 | 530 | |
c3350683 | 531 | if (m->scope_job && manager_job_is_active(m->manager, m->scope_job)) |
554ce41f | 532 | return false; |
9444b1f2 | 533 | |
89f7c846 | 534 | if (m->unit && manager_unit_is_active(m->manager, m->unit)) |
554ce41f | 535 | return false; |
9444b1f2 | 536 | |
554ce41f | 537 | return true; |
9444b1f2 LP |
538 | } |
539 | ||
540 | void machine_add_to_gc_queue(Machine *m) { | |
541 | assert(m); | |
542 | ||
543 | if (m->in_gc_queue) | |
544 | return; | |
545 | ||
71fda00f | 546 | LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 LP |
547 | m->in_gc_queue = true; |
548 | } | |
549 | ||
fb6becb4 LP |
550 | MachineState machine_get_state(Machine *s) { |
551 | assert(s); | |
9444b1f2 | 552 | |
fbe55073 LP |
553 | if (s->class == MACHINE_HOST) |
554 | return MACHINE_RUNNING; | |
555 | ||
49f3fffd LP |
556 | if (s->stopping) |
557 | return MACHINE_CLOSING; | |
558 | ||
fb6becb4 | 559 | if (s->scope_job) |
49f3fffd | 560 | return MACHINE_OPENING; |
9444b1f2 | 561 | |
fb6becb4 LP |
562 | return MACHINE_RUNNING; |
563 | } | |
9444b1f2 | 564 | |
fb6becb4 LP |
565 | int machine_kill(Machine *m, KillWho who, int signo) { |
566 | assert(m); | |
9444b1f2 | 567 | |
fbe55073 LP |
568 | if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER)) |
569 | return -EOPNOTSUPP; | |
570 | ||
89f7c846 | 571 | if (!m->unit) |
fb6becb4 | 572 | return -ESRCH; |
9444b1f2 | 573 | |
7c248223 LP |
574 | if (who == KILL_LEADER) /* If we shall simply kill the leader, do so directly */ |
575 | return RET_NERRNO(kill(m->leader, signo)); | |
de58a50e | 576 | |
b938cb90 | 577 | /* Otherwise, make PID 1 do it for us, for the entire cgroup */ |
de58a50e | 578 | return manager_kill_unit(m->manager, m->unit, signo, NULL); |
9444b1f2 LP |
579 | } |
580 | ||
ae1d13db | 581 | int machine_openpt(Machine *m, int flags, char **ret_slave) { |
fbe55073 LP |
582 | assert(m); |
583 | ||
584 | switch (m->class) { | |
585 | ||
ae1d13db | 586 | case MACHINE_HOST: |
5f430ff7 | 587 | |
ae1d13db | 588 | return openpt_allocate(flags, ret_slave); |
fbe55073 LP |
589 | |
590 | case MACHINE_CONTAINER: | |
591 | if (m->leader <= 0) | |
592 | return -EINVAL; | |
593 | ||
ae1d13db | 594 | return openpt_allocate_in_namespace(m->leader, flags, ret_slave); |
fbe55073 LP |
595 | |
596 | default: | |
597 | return -EOPNOTSUPP; | |
598 | } | |
599 | } | |
600 | ||
40e1f4ea LP |
601 | int machine_open_terminal(Machine *m, const char *path, int mode) { |
602 | assert(m); | |
603 | ||
604 | switch (m->class) { | |
605 | ||
606 | case MACHINE_HOST: | |
607 | return open_terminal(path, mode); | |
608 | ||
609 | case MACHINE_CONTAINER: | |
610 | if (m->leader <= 0) | |
611 | return -EINVAL; | |
612 | ||
613 | return open_terminal_in_namespace(m->leader, path, mode); | |
614 | ||
615 | default: | |
616 | return -EOPNOTSUPP; | |
617 | } | |
618 | } | |
619 | ||
9b420b3c LP |
620 | void machine_release_unit(Machine *m) { |
621 | assert(m); | |
622 | ||
623 | if (!m->unit) | |
624 | return; | |
625 | ||
eec12b77 ZJS |
626 | if (m->referenced) { |
627 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
628 | int r; | |
629 | ||
630 | r = manager_unref_unit(m->manager, m->unit, &error); | |
631 | if (r < 0) | |
632 | log_warning_errno(r, "Failed to drop reference to machine scope, ignoring: %s", | |
633 | bus_error_message(&error, r)); | |
634 | ||
635 | m->referenced = false; | |
636 | } | |
637 | ||
9b420b3c | 638 | (void) hashmap_remove(m->manager->machine_units, m->unit); |
a1e58e8e | 639 | m->unit = mfree(m->unit); |
9b420b3c LP |
640 | } |
641 | ||
3401419b | 642 | int machine_get_uid_shift(Machine *m, uid_t *ret) { |
fbd0b64f | 643 | char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
3401419b LP |
644 | uid_t uid_base, uid_shift, uid_range; |
645 | gid_t gid_base, gid_shift, gid_range; | |
646 | _cleanup_fclose_ FILE *f = NULL; | |
03a7dbea | 647 | int k, r; |
3401419b LP |
648 | |
649 | assert(m); | |
650 | assert(ret); | |
651 | ||
652 | /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple | |
653 | * mappings. In most cases setups should be simple like this, and administrators should only care about the | |
654 | * basic offset a container has relative to the host. This is what this function exposes. | |
655 | * | |
656 | * If we encounter any more complex mappings we politely refuse this with ENXIO. */ | |
657 | ||
658 | if (m->class == MACHINE_HOST) { | |
659 | *ret = 0; | |
660 | return 0; | |
661 | } | |
662 | ||
663 | if (m->class != MACHINE_CONTAINER) | |
664 | return -EOPNOTSUPP; | |
665 | ||
666 | xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader); | |
667 | f = fopen(p, "re"); | |
668 | if (!f) { | |
669 | if (errno == ENOENT) { | |
670 | /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */ | |
671 | *ret = 0; | |
672 | return 0; | |
673 | } | |
674 | ||
675 | return -errno; | |
676 | } | |
677 | ||
678 | /* Read the first line. There's at least one. */ | |
679 | errno = 0; | |
680 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range); | |
681 | if (k != 3) { | |
682 | if (ferror(f)) | |
66855de7 | 683 | return errno_or_else(EIO); |
3401419b LP |
684 | |
685 | return -EBADMSG; | |
686 | } | |
687 | ||
688 | /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */ | |
689 | if (uid_base != 0) | |
690 | return -ENXIO; | |
691 | /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */ | |
3a664727 | 692 | if (uid_range < UID_NOBODY) |
3401419b LP |
693 | return -ENXIO; |
694 | ||
695 | /* If there's more than one line, then we don't support this mapping. */ | |
03a7dbea LP |
696 | r = safe_fgetc(f, NULL); |
697 | if (r < 0) | |
698 | return r; | |
699 | if (r != 0) /* Insist on EOF */ | |
3401419b LP |
700 | return -ENXIO; |
701 | ||
702 | fclose(f); | |
703 | ||
704 | xsprintf(p, "/proc/" PID_FMT "/gid_map", m->leader); | |
705 | f = fopen(p, "re"); | |
706 | if (!f) | |
707 | return -errno; | |
708 | ||
709 | /* Read the first line. There's at least one. */ | |
710 | errno = 0; | |
711 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range); | |
712 | if (k != 3) { | |
713 | if (ferror(f)) | |
66855de7 | 714 | return errno_or_else(EIO); |
3401419b LP |
715 | |
716 | return -EBADMSG; | |
717 | } | |
718 | ||
719 | /* If there's more than one line, then we don't support this file. */ | |
03a7dbea LP |
720 | r = safe_fgetc(f, NULL); |
721 | if (r < 0) | |
722 | return r; | |
723 | if (r != 0) /* Insist on EOF */ | |
3401419b LP |
724 | return -ENXIO; |
725 | ||
726 | /* If the UID and GID mapping doesn't match, we don't support this mapping. */ | |
727 | if (uid_base != (uid_t) gid_base) | |
728 | return -ENXIO; | |
729 | if (uid_shift != (uid_t) gid_shift) | |
730 | return -ENXIO; | |
731 | if (uid_range != (uid_t) gid_range) | |
732 | return -ENXIO; | |
733 | ||
734 | *ret = uid_shift; | |
735 | return 0; | |
736 | } | |
737 | ||
74d1b7d2 LP |
738 | static int machine_owns_uid_internal( |
739 | Machine *machine, | |
740 | const char *map_file, /* "uid_map" or "gid_map" */ | |
741 | uid_t uid, | |
742 | uid_t *ret_internal_uid) { | |
743 | ||
744 | _cleanup_fclose_ FILE *f = NULL; | |
745 | const char *p; | |
746 | ||
747 | /* This is a generic implementation for both uids and gids, under the assumptions they have the same types and semantics. */ | |
748 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
749 | ||
750 | assert(machine); | |
751 | ||
752 | /* Checks if the specified host UID is owned by the machine, and returns the UID it maps to | |
753 | * internally in the machine */ | |
754 | ||
755 | if (machine->class != MACHINE_CONTAINER) | |
756 | goto negative; | |
757 | ||
758 | p = procfs_file_alloca(machine->leader, map_file); | |
759 | f = fopen(p, "re"); | |
760 | if (!f) { | |
761 | log_debug_errno(errno, "Failed to open %s, ignoring.", p); | |
762 | goto negative; | |
763 | } | |
764 | ||
765 | for (;;) { | |
766 | uid_t uid_base, uid_shift, uid_range, converted; | |
767 | int k; | |
768 | ||
769 | errno = 0; | |
770 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range); | |
771 | if (k < 0 && feof(f)) | |
772 | break; | |
773 | if (k != 3) { | |
774 | if (ferror(f)) | |
775 | return errno_or_else(EIO); | |
776 | ||
777 | return -EIO; | |
778 | } | |
779 | ||
780 | /* The private user namespace is disabled, ignoring. */ | |
781 | if (uid_shift == 0) | |
782 | continue; | |
783 | ||
784 | if (uid < uid_shift || uid >= uid_shift + uid_range) | |
785 | continue; | |
786 | ||
787 | converted = (uid - uid_shift + uid_base); | |
788 | if (!uid_is_valid(converted)) | |
789 | return -EINVAL; | |
790 | ||
791 | if (ret_internal_uid) | |
792 | *ret_internal_uid = converted; | |
793 | ||
794 | return true; | |
795 | } | |
796 | ||
797 | negative: | |
798 | if (ret_internal_uid) | |
799 | *ret_internal_uid = UID_INVALID; | |
800 | ||
801 | return false; | |
802 | } | |
803 | ||
804 | int machine_owns_uid(Machine *machine, uid_t uid, uid_t *ret_internal_uid) { | |
805 | return machine_owns_uid_internal(machine, "uid_map", uid, ret_internal_uid); | |
806 | } | |
807 | ||
808 | int machine_owns_gid(Machine *machine, gid_t gid, gid_t *ret_internal_gid) { | |
809 | return machine_owns_uid_internal(machine, "gid_map", (uid_t) gid, (uid_t*) ret_internal_gid); | |
810 | } | |
811 | ||
812 | static int machine_translate_uid_internal( | |
813 | Machine *machine, | |
814 | const char *map_file, /* "uid_map" or "gid_map" */ | |
815 | uid_t uid, | |
816 | uid_t *ret_host_uid) { | |
817 | ||
818 | _cleanup_fclose_ FILE *f = NULL; | |
819 | const char *p; | |
820 | ||
821 | /* This is a generic implementation for both uids and gids, under the assumptions they have the same types and semantics. */ | |
822 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
823 | ||
824 | assert(machine); | |
825 | assert(uid_is_valid(uid)); | |
826 | ||
827 | if (machine->class != MACHINE_CONTAINER) | |
828 | return -ESRCH; | |
829 | ||
830 | /* Translates a machine UID into a host UID */ | |
831 | ||
832 | p = procfs_file_alloca(machine->leader, map_file); | |
833 | f = fopen(p, "re"); | |
834 | if (!f) | |
835 | return -errno; | |
836 | ||
837 | for (;;) { | |
838 | uid_t uid_base, uid_shift, uid_range, converted; | |
839 | int k; | |
840 | ||
841 | errno = 0; | |
842 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range); | |
843 | if (k < 0 && feof(f)) | |
844 | break; | |
845 | if (k != 3) { | |
846 | if (ferror(f)) | |
847 | return errno_or_else(EIO); | |
848 | ||
849 | return -EIO; | |
850 | } | |
851 | ||
852 | if (uid < uid_base || uid >= uid_base + uid_range) | |
853 | continue; | |
854 | ||
855 | converted = uid - uid_base + uid_shift; | |
856 | if (!uid_is_valid(converted)) | |
857 | return -EINVAL; | |
858 | ||
859 | if (ret_host_uid) | |
860 | *ret_host_uid = converted; | |
861 | return 0; | |
862 | } | |
863 | ||
864 | return -ESRCH; | |
865 | } | |
866 | ||
867 | int machine_translate_uid(Machine *machine, gid_t uid, gid_t *ret_host_uid) { | |
868 | return machine_translate_uid_internal(machine, "uid_map", uid, ret_host_uid); | |
869 | } | |
870 | ||
871 | int machine_translate_gid(Machine *machine, gid_t gid, gid_t *ret_host_gid) { | |
872 | return machine_translate_uid_internal(machine, "gid_map", (uid_t) gid, (uid_t*) ret_host_gid); | |
873 | } | |
874 | ||
9444b1f2 LP |
875 | static const char* const machine_class_table[_MACHINE_CLASS_MAX] = { |
876 | [MACHINE_CONTAINER] = "container", | |
fbe55073 LP |
877 | [MACHINE_VM] = "vm", |
878 | [MACHINE_HOST] = "host", | |
9444b1f2 LP |
879 | }; |
880 | ||
881 | DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass); | |
fb6becb4 LP |
882 | |
883 | static const char* const machine_state_table[_MACHINE_STATE_MAX] = { | |
884 | [MACHINE_OPENING] = "opening", | |
885 | [MACHINE_RUNNING] = "running", | |
886 | [MACHINE_CLOSING] = "closing" | |
887 | }; | |
888 | ||
889 | DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState); | |
1ee306e1 LP |
890 | |
891 | static const char* const kill_who_table[_KILL_WHO_MAX] = { | |
892 | [KILL_LEADER] = "leader", | |
893 | [KILL_ALL] = "all" | |
894 | }; | |
895 | ||
896 | DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho); |