]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 LP |
10 | #include "bus-error.h" |
11 | #include "bus-util.h" | |
686d13b9 | 12 | #include "env-file.h" |
66855de7 | 13 | #include "errno-util.h" |
4f5dd394 | 14 | #include "escape.h" |
cf0fbc49 | 15 | #include "extract-word.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
9444b1f2 | 17 | #include "fileio.h" |
f97b34a6 | 18 | #include "format-util.h" |
66cb2fde | 19 | #include "hashmap.h" |
4f5dd394 | 20 | #include "machine-dbus.h" |
3ffd4af2 | 21 | #include "machine.h" |
66cb2fde | 22 | #include "mkdir.h" |
6bedfcbb | 23 | #include "parse-util.h" |
b910cc72 | 24 | #include "path-util.h" |
4a0b58c4 | 25 | #include "process-util.h" |
d68c645b | 26 | #include "serialize.h" |
9444b1f2 | 27 | #include "special.h" |
3401419b | 28 | #include "stdio-util.h" |
8b43440b | 29 | #include "string-table.h" |
66cb2fde | 30 | #include "terminal-util.h" |
e4de7287 | 31 | #include "tmpfile-util.h" |
fb6becb4 | 32 | #include "unit-name.h" |
3a664727 | 33 | #include "user-util.h" |
66cb2fde | 34 | #include "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 LP |
136 | if (m->unit) { |
137 | _cleanup_free_ char *escaped; | |
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 LP |
151 | if (m->service) { |
152 | _cleanup_free_ char *escaped; | |
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) { | |
163 | _cleanup_free_ char *escaped; | |
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); |
9444b1f2 LP |
268 | if (r < 0) { |
269 | if (r == -ENOENT) | |
270 | return 0; | |
271 | ||
8d3d7072 | 272 | return log_error_errno(r, "Failed to read %s: %m", m->state_file); |
9444b1f2 LP |
273 | } |
274 | ||
275 | if (id) | |
276 | sd_id128_from_string(id, &m->id); | |
277 | ||
278 | if (leader) | |
279 | parse_pid(leader, &m->leader); | |
280 | ||
281 | if (class) { | |
282 | MachineClass c; | |
283 | ||
284 | c = machine_class_from_string(class); | |
285 | if (c >= 0) | |
286 | m->class = c; | |
287 | } | |
288 | ||
b895a735 | 289 | if (realtime) |
d68c645b | 290 | (void) deserialize_usec(realtime, &m->timestamp.realtime); |
b895a735 | 291 | if (monotonic) |
d68c645b | 292 | (void) deserialize_usec(monotonic, &m->timestamp.monotonic); |
9444b1f2 | 293 | |
9b5ed6fe | 294 | if (netif) { |
75a8fd6a SS |
295 | size_t allocated = 0, nr = 0; |
296 | const char *p; | |
9b5ed6fe LP |
297 | int *ni = NULL; |
298 | ||
75a8fd6a | 299 | p = netif; |
9ed794a3 | 300 | for (;;) { |
75a8fd6a | 301 | _cleanup_free_ char *word = NULL; |
9b5ed6fe LP |
302 | int ifi; |
303 | ||
75a8fd6a | 304 | r = extract_first_word(&p, &word, NULL, 0); |
75a8fd6a SS |
305 | if (r == 0) |
306 | break; | |
6a37c684 | 307 | if (r == -ENOMEM) |
52278ad3 | 308 | return log_oom(); |
6a37c684 | 309 | if (r < 0) { |
52278ad3 | 310 | log_warning_errno(r, "Failed to parse NETIF: %s", netif); |
6a37c684 | 311 | break; |
52278ad3 | 312 | } |
75a8fd6a | 313 | |
6ad623a3 | 314 | if (parse_ifindex(word, &ifi) < 0) |
9b5ed6fe LP |
315 | continue; |
316 | ||
317 | if (!GREEDY_REALLOC(ni, allocated, nr+1)) { | |
318 | free(ni); | |
319 | return log_oom(); | |
320 | } | |
321 | ||
322 | ni[nr++] = ifi; | |
323 | } | |
324 | ||
325 | free(m->netif); | |
326 | m->netif = ni; | |
327 | m->n_netif = nr; | |
328 | } | |
329 | ||
9444b1f2 LP |
330 | return r; |
331 | } | |
332 | ||
af227947 | 333 | static int machine_start_scope( |
a01ecfa9 | 334 | Machine *machine, |
af227947 | 335 | sd_bus_message *more_properties, |
a01ecfa9 | 336 | sd_bus_error *error) { |
af227947 ZJS |
337 | |
338 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
a01ecfa9 ZJS |
339 | _cleanup_free_ char *escaped = NULL, *unit = NULL; |
340 | const char *description; | |
af227947 ZJS |
341 | int r; |
342 | ||
a01ecfa9 ZJS |
343 | assert(machine); |
344 | assert(machine->leader > 0); | |
345 | assert(!machine->unit); | |
346 | ||
347 | escaped = unit_name_escape(machine->name); | |
348 | if (!escaped) | |
349 | return log_oom(); | |
350 | ||
351 | unit = strjoin("machine-", escaped, ".scope"); | |
352 | if (!unit) | |
353 | return log_oom(); | |
af227947 ZJS |
354 | |
355 | r = sd_bus_message_new_method_call( | |
a01ecfa9 | 356 | machine->manager->bus, |
af227947 ZJS |
357 | &m, |
358 | "org.freedesktop.systemd1", | |
359 | "/org/freedesktop/systemd1", | |
360 | "org.freedesktop.systemd1.Manager", | |
361 | "StartTransientUnit"); | |
362 | if (r < 0) | |
363 | return r; | |
364 | ||
a01ecfa9 | 365 | r = sd_bus_message_append(m, "ss", unit, "fail"); |
af227947 ZJS |
366 | if (r < 0) |
367 | return r; | |
368 | ||
369 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
370 | if (r < 0) | |
371 | return r; | |
372 | ||
a01ecfa9 ZJS |
373 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", SPECIAL_MACHINE_SLICE); |
374 | if (r < 0) | |
375 | return r; | |
af227947 | 376 | |
a01ecfa9 ZJS |
377 | description = strjoina(machine->class == MACHINE_VM ? "Virtual Machine " : "Container ", machine->name); |
378 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); | |
379 | if (r < 0) | |
380 | return r; | |
af227947 ZJS |
381 | |
382 | r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)", | |
a01ecfa9 | 383 | "PIDs", "au", 1, machine->leader, |
af227947 ZJS |
384 | "Delegate", "b", 1, |
385 | "CollectMode", "s", "inactive-or-failed", | |
386 | "AddRef", "b", 1, | |
387 | "TasksMax", "t", UINT64_C(16384)); | |
388 | if (r < 0) | |
389 | return r; | |
390 | ||
391 | if (more_properties) { | |
392 | r = sd_bus_message_copy(m, more_properties, true); | |
393 | if (r < 0) | |
394 | return r; | |
395 | } | |
396 | ||
397 | r = sd_bus_message_close_container(m); | |
398 | if (r < 0) | |
399 | return r; | |
400 | ||
401 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
402 | if (r < 0) | |
403 | return r; | |
404 | ||
a01ecfa9 | 405 | r = sd_bus_call(NULL, m, 0, error, &reply); |
af227947 ZJS |
406 | if (r < 0) |
407 | return r; | |
408 | ||
a01ecfa9 ZJS |
409 | machine->unit = TAKE_PTR(unit); |
410 | machine->referenced = true; | |
af227947 | 411 | |
a01ecfa9 ZJS |
412 | const char *job; |
413 | r = sd_bus_message_read(reply, "o", &job); | |
414 | if (r < 0) | |
415 | return r; | |
af227947 | 416 | |
a01ecfa9 | 417 | return free_and_strdup(&machine->scope_job, job); |
af227947 ZJS |
418 | } |
419 | ||
420 | static int machine_ensure_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) { | |
a01ecfa9 ZJS |
421 | int r; |
422 | ||
9444b1f2 | 423 | assert(m); |
fbe55073 | 424 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 425 | |
89f7c846 | 426 | if (!m->unit) { |
a01ecfa9 | 427 | r = machine_start_scope(m, properties, error); |
354f62cf YW |
428 | if (r < 0) |
429 | return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r)); | |
9444b1f2 LP |
430 | } |
431 | ||
a01ecfa9 ZJS |
432 | assert(m->unit); |
433 | hashmap_put(m->manager->machine_units, m->unit, m); | |
d0af76e6 | 434 | |
354f62cf | 435 | return 0; |
9444b1f2 LP |
436 | } |
437 | ||
c3350683 | 438 | int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 LP |
439 | int r; |
440 | ||
441 | assert(m); | |
442 | ||
fbe55073 LP |
443 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
444 | return -EOPNOTSUPP; | |
445 | ||
9444b1f2 LP |
446 | if (m->started) |
447 | return 0; | |
448 | ||
4a0b58c4 | 449 | r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb LP |
450 | if (r < 0) |
451 | return r; | |
452 | ||
fb6becb4 | 453 | /* Create cgroup */ |
af227947 | 454 | r = machine_ensure_scope(m, properties, error); |
fb6becb4 LP |
455 | if (r < 0) |
456 | return r; | |
457 | ||
9444b1f2 | 458 | log_struct(LOG_INFO, |
2b044526 | 459 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR, |
9444b1f2 | 460 | "NAME=%s", m->name, |
de0671ee | 461 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 462 | LOG_MESSAGE("New machine %s.", m->name)); |
9444b1f2 | 463 | |
9444b1f2 LP |
464 | if (!dual_timestamp_is_set(&m->timestamp)) |
465 | dual_timestamp_get(&m->timestamp); | |
466 | ||
467 | m->started = true; | |
468 | ||
469 | /* Save new machine data */ | |
470 | machine_save(m); | |
471 | ||
472 | machine_send_signal(m, true); | |
9fdcbae5 | 473 | (void) manager_enqueue_nscd_cache_flush(m->manager); |
9444b1f2 LP |
474 | |
475 | return 0; | |
476 | } | |
477 | ||
9444b1f2 | 478 | int machine_stop(Machine *m) { |
49f3fffd | 479 | int r; |
69887664 | 480 | |
49f3fffd LP |
481 | assert(m); |
482 | ||
fbe55073 LP |
483 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
484 | return -EOPNOTSUPP; | |
485 | ||
69887664 ZJS |
486 | if (m->unit) { |
487 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
488 | char *job = NULL; | |
489 | ||
490 | r = manager_stop_unit(m->manager, m->unit, &error, &job); | |
491 | if (r < 0) | |
492 | return log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r)); | |
493 | ||
494 | free_and_replace(m->scope_job, job); | |
495 | } | |
49f3fffd LP |
496 | |
497 | m->stopping = true; | |
498 | ||
499 | machine_save(m); | |
9fdcbae5 | 500 | (void) manager_enqueue_nscd_cache_flush(m->manager); |
49f3fffd | 501 | |
69887664 | 502 | return 0; |
49f3fffd LP |
503 | } |
504 | ||
505 | int machine_finalize(Machine *m) { | |
9444b1f2 LP |
506 | assert(m); |
507 | ||
ef8ff92e | 508 | if (m->started) { |
9444b1f2 | 509 | log_struct(LOG_INFO, |
2b044526 | 510 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR, |
9444b1f2 | 511 | "NAME=%s", m->name, |
de0671ee | 512 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 513 | LOG_MESSAGE("Machine %s terminated.", m->name)); |
9444b1f2 | 514 | |
ef8ff92e ZJS |
515 | m->stopping = true; /* The machine is supposed to be going away. Don't try to kill it. */ |
516 | } | |
517 | ||
89f7c846 | 518 | machine_unlink(m); |
9444b1f2 LP |
519 | machine_add_to_gc_queue(m); |
520 | ||
49f3fffd | 521 | if (m->started) { |
9444b1f2 | 522 | machine_send_signal(m, false); |
49f3fffd LP |
523 | m->started = false; |
524 | } | |
9444b1f2 | 525 | |
49f3fffd | 526 | return 0; |
9444b1f2 LP |
527 | } |
528 | ||
554ce41f | 529 | bool machine_may_gc(Machine *m, bool drop_not_started) { |
9444b1f2 LP |
530 | assert(m); |
531 | ||
fbe55073 | 532 | if (m->class == MACHINE_HOST) |
554ce41f | 533 | return false; |
fbe55073 | 534 | |
9444b1f2 | 535 | if (drop_not_started && !m->started) |
554ce41f | 536 | return true; |
9444b1f2 | 537 | |
c3350683 | 538 | if (m->scope_job && manager_job_is_active(m->manager, m->scope_job)) |
554ce41f | 539 | return false; |
9444b1f2 | 540 | |
89f7c846 | 541 | if (m->unit && manager_unit_is_active(m->manager, m->unit)) |
554ce41f | 542 | return false; |
9444b1f2 | 543 | |
554ce41f | 544 | return true; |
9444b1f2 LP |
545 | } |
546 | ||
547 | void machine_add_to_gc_queue(Machine *m) { | |
548 | assert(m); | |
549 | ||
550 | if (m->in_gc_queue) | |
551 | return; | |
552 | ||
71fda00f | 553 | LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 LP |
554 | m->in_gc_queue = true; |
555 | } | |
556 | ||
fb6becb4 LP |
557 | MachineState machine_get_state(Machine *s) { |
558 | assert(s); | |
9444b1f2 | 559 | |
fbe55073 LP |
560 | if (s->class == MACHINE_HOST) |
561 | return MACHINE_RUNNING; | |
562 | ||
49f3fffd LP |
563 | if (s->stopping) |
564 | return MACHINE_CLOSING; | |
565 | ||
fb6becb4 | 566 | if (s->scope_job) |
49f3fffd | 567 | return MACHINE_OPENING; |
9444b1f2 | 568 | |
fb6becb4 LP |
569 | return MACHINE_RUNNING; |
570 | } | |
9444b1f2 | 571 | |
fb6becb4 LP |
572 | int machine_kill(Machine *m, KillWho who, int signo) { |
573 | assert(m); | |
9444b1f2 | 574 | |
fbe55073 LP |
575 | if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER)) |
576 | return -EOPNOTSUPP; | |
577 | ||
89f7c846 | 578 | if (!m->unit) |
fb6becb4 | 579 | return -ESRCH; |
9444b1f2 | 580 | |
de58a50e LP |
581 | if (who == KILL_LEADER) { |
582 | /* If we shall simply kill the leader, do so directly */ | |
583 | ||
584 | if (kill(m->leader, signo) < 0) | |
585 | return -errno; | |
9d685ca8 ED |
586 | |
587 | return 0; | |
de58a50e LP |
588 | } |
589 | ||
b938cb90 | 590 | /* Otherwise, make PID 1 do it for us, for the entire cgroup */ |
de58a50e | 591 | return manager_kill_unit(m->manager, m->unit, signo, NULL); |
9444b1f2 LP |
592 | } |
593 | ||
ae1d13db | 594 | int machine_openpt(Machine *m, int flags, char **ret_slave) { |
fbe55073 LP |
595 | assert(m); |
596 | ||
597 | switch (m->class) { | |
598 | ||
ae1d13db | 599 | case MACHINE_HOST: |
5f430ff7 | 600 | |
ae1d13db | 601 | return openpt_allocate(flags, ret_slave); |
fbe55073 LP |
602 | |
603 | case MACHINE_CONTAINER: | |
604 | if (m->leader <= 0) | |
605 | return -EINVAL; | |
606 | ||
ae1d13db | 607 | return openpt_allocate_in_namespace(m->leader, flags, ret_slave); |
fbe55073 LP |
608 | |
609 | default: | |
610 | return -EOPNOTSUPP; | |
611 | } | |
612 | } | |
613 | ||
40e1f4ea LP |
614 | int machine_open_terminal(Machine *m, const char *path, int mode) { |
615 | assert(m); | |
616 | ||
617 | switch (m->class) { | |
618 | ||
619 | case MACHINE_HOST: | |
620 | return open_terminal(path, mode); | |
621 | ||
622 | case MACHINE_CONTAINER: | |
623 | if (m->leader <= 0) | |
624 | return -EINVAL; | |
625 | ||
626 | return open_terminal_in_namespace(m->leader, path, mode); | |
627 | ||
628 | default: | |
629 | return -EOPNOTSUPP; | |
630 | } | |
631 | } | |
632 | ||
9b420b3c LP |
633 | void machine_release_unit(Machine *m) { |
634 | assert(m); | |
635 | ||
636 | if (!m->unit) | |
637 | return; | |
638 | ||
eec12b77 ZJS |
639 | if (m->referenced) { |
640 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
641 | int r; | |
642 | ||
643 | r = manager_unref_unit(m->manager, m->unit, &error); | |
644 | if (r < 0) | |
645 | log_warning_errno(r, "Failed to drop reference to machine scope, ignoring: %s", | |
646 | bus_error_message(&error, r)); | |
647 | ||
648 | m->referenced = false; | |
649 | } | |
650 | ||
9b420b3c | 651 | (void) hashmap_remove(m->manager->machine_units, m->unit); |
a1e58e8e | 652 | m->unit = mfree(m->unit); |
9b420b3c LP |
653 | } |
654 | ||
3401419b | 655 | int machine_get_uid_shift(Machine *m, uid_t *ret) { |
fbd0b64f | 656 | char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
3401419b LP |
657 | uid_t uid_base, uid_shift, uid_range; |
658 | gid_t gid_base, gid_shift, gid_range; | |
659 | _cleanup_fclose_ FILE *f = NULL; | |
03a7dbea | 660 | int k, r; |
3401419b LP |
661 | |
662 | assert(m); | |
663 | assert(ret); | |
664 | ||
665 | /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple | |
666 | * mappings. In most cases setups should be simple like this, and administrators should only care about the | |
667 | * basic offset a container has relative to the host. This is what this function exposes. | |
668 | * | |
669 | * If we encounter any more complex mappings we politely refuse this with ENXIO. */ | |
670 | ||
671 | if (m->class == MACHINE_HOST) { | |
672 | *ret = 0; | |
673 | return 0; | |
674 | } | |
675 | ||
676 | if (m->class != MACHINE_CONTAINER) | |
677 | return -EOPNOTSUPP; | |
678 | ||
679 | xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader); | |
680 | f = fopen(p, "re"); | |
681 | if (!f) { | |
682 | if (errno == ENOENT) { | |
683 | /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */ | |
684 | *ret = 0; | |
685 | return 0; | |
686 | } | |
687 | ||
688 | return -errno; | |
689 | } | |
690 | ||
691 | /* Read the first line. There's at least one. */ | |
692 | errno = 0; | |
693 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range); | |
694 | if (k != 3) { | |
695 | if (ferror(f)) | |
66855de7 | 696 | return errno_or_else(EIO); |
3401419b LP |
697 | |
698 | return -EBADMSG; | |
699 | } | |
700 | ||
701 | /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */ | |
702 | if (uid_base != 0) | |
703 | return -ENXIO; | |
704 | /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */ | |
3a664727 | 705 | if (uid_range < UID_NOBODY) |
3401419b LP |
706 | return -ENXIO; |
707 | ||
708 | /* If there's more than one line, then we don't support this mapping. */ | |
03a7dbea LP |
709 | r = safe_fgetc(f, NULL); |
710 | if (r < 0) | |
711 | return r; | |
712 | if (r != 0) /* Insist on EOF */ | |
3401419b LP |
713 | return -ENXIO; |
714 | ||
715 | fclose(f); | |
716 | ||
717 | xsprintf(p, "/proc/" PID_FMT "/gid_map", m->leader); | |
718 | f = fopen(p, "re"); | |
719 | if (!f) | |
720 | return -errno; | |
721 | ||
722 | /* Read the first line. There's at least one. */ | |
723 | errno = 0; | |
724 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range); | |
725 | if (k != 3) { | |
726 | if (ferror(f)) | |
66855de7 | 727 | return errno_or_else(EIO); |
3401419b LP |
728 | |
729 | return -EBADMSG; | |
730 | } | |
731 | ||
732 | /* If there's more than one line, then we don't support this file. */ | |
03a7dbea LP |
733 | r = safe_fgetc(f, NULL); |
734 | if (r < 0) | |
735 | return r; | |
736 | if (r != 0) /* Insist on EOF */ | |
3401419b LP |
737 | return -ENXIO; |
738 | ||
739 | /* If the UID and GID mapping doesn't match, we don't support this mapping. */ | |
740 | if (uid_base != (uid_t) gid_base) | |
741 | return -ENXIO; | |
742 | if (uid_shift != (uid_t) gid_shift) | |
743 | return -ENXIO; | |
744 | if (uid_range != (uid_t) gid_range) | |
745 | return -ENXIO; | |
746 | ||
747 | *ret = uid_shift; | |
748 | return 0; | |
749 | } | |
750 | ||
9444b1f2 LP |
751 | static const char* const machine_class_table[_MACHINE_CLASS_MAX] = { |
752 | [MACHINE_CONTAINER] = "container", | |
fbe55073 LP |
753 | [MACHINE_VM] = "vm", |
754 | [MACHINE_HOST] = "host", | |
9444b1f2 LP |
755 | }; |
756 | ||
757 | DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass); | |
fb6becb4 LP |
758 | |
759 | static const char* const machine_state_table[_MACHINE_STATE_MAX] = { | |
760 | [MACHINE_OPENING] = "opening", | |
761 | [MACHINE_RUNNING] = "running", | |
762 | [MACHINE_CLOSING] = "closing" | |
763 | }; | |
764 | ||
765 | DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState); | |
1ee306e1 LP |
766 | |
767 | static const char* const kill_who_table[_KILL_WHO_MAX] = { | |
768 | [KILL_LEADER] = "leader", | |
769 | [KILL_ALL] = "all" | |
770 | }; | |
771 | ||
772 | DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho); |