]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9444b1f2 | 2 | |
66cb2fde | 3 | #include <errno.h> |
9444b1f2 LP |
4 | #include <string.h> |
5 | #include <unistd.h> | |
0d536673 | 6 | #include <stdio_ext.h> |
9444b1f2 | 7 | |
c3350683 | 8 | #include "sd-messages.h" |
fb6becb4 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
66cb2fde LP |
11 | #include "bus-error.h" |
12 | #include "bus-util.h" | |
686d13b9 | 13 | #include "env-file.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" |
4a0b58c4 | 24 | #include "process-util.h" |
d68c645b | 25 | #include "serialize.h" |
9444b1f2 | 26 | #include "special.h" |
3401419b | 27 | #include "stdio-util.h" |
8b43440b | 28 | #include "string-table.h" |
66cb2fde | 29 | #include "terminal-util.h" |
e4de7287 | 30 | #include "tmpfile-util.h" |
fb6becb4 | 31 | #include "unit-name.h" |
3a664727 | 32 | #include "user-util.h" |
66cb2fde | 33 | #include "util.h" |
9444b1f2 | 34 | |
fbe55073 | 35 | Machine* machine_new(Manager *manager, MachineClass class, const char *name) { |
9444b1f2 LP |
36 | Machine *m; |
37 | ||
38 | assert(manager); | |
fbe55073 | 39 | assert(class < _MACHINE_CLASS_MAX); |
9444b1f2 LP |
40 | assert(name); |
41 | ||
fbe55073 LP |
42 | /* Passing class == _MACHINE_CLASS_INVALID here is fine. It |
43 | * means as much as "we don't know yet", and that we'll figure | |
44 | * it out later when loading the state file. */ | |
45 | ||
9444b1f2 LP |
46 | m = new0(Machine, 1); |
47 | if (!m) | |
48 | return NULL; | |
49 | ||
50 | m->name = strdup(name); | |
51 | if (!m->name) | |
52 | goto fail; | |
53 | ||
fbe55073 LP |
54 | if (class != MACHINE_HOST) { |
55 | m->state_file = strappend("/run/systemd/machines/", m->name); | |
56 | if (!m->state_file) | |
57 | goto fail; | |
58 | } | |
59 | ||
60 | m->class = class; | |
9444b1f2 LP |
61 | |
62 | if (hashmap_put(manager->machines, m->name, m) < 0) | |
63 | goto fail; | |
64 | ||
9444b1f2 LP |
65 | m->manager = manager; |
66 | ||
67 | return m; | |
68 | ||
69 | fail: | |
70 | free(m->state_file); | |
71 | free(m->name); | |
6b430fdb | 72 | return mfree(m); |
9444b1f2 LP |
73 | } |
74 | ||
bb1a05d6 YW |
75 | Machine* machine_free(Machine *m) { |
76 | if (!m) | |
77 | return NULL; | |
9444b1f2 | 78 | |
0370612e | 79 | while (m->operations) |
795c5d31 | 80 | operation_free(m->operations); |
0370612e | 81 | |
9444b1f2 | 82 | if (m->in_gc_queue) |
71fda00f | 83 | LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 | 84 | |
9b420b3c | 85 | machine_release_unit(m); |
9444b1f2 | 86 | |
fb6becb4 LP |
87 | free(m->scope_job); |
88 | ||
9b420b3c | 89 | (void) hashmap_remove(m->manager->machines, m->name); |
9444b1f2 | 90 | |
fbe55073 LP |
91 | if (m->manager->host_machine == m) |
92 | m->manager->host_machine = NULL; | |
93 | ||
d3e84ddb | 94 | if (m->leader > 0) |
4a0b58c4 | 95 | (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb | 96 | |
c3350683 | 97 | sd_bus_message_unref(m->create_message); |
fb6becb4 | 98 | |
9444b1f2 LP |
99 | free(m->name); |
100 | free(m->state_file); | |
101 | free(m->service); | |
9444b1f2 | 102 | free(m->root_directory); |
9b5ed6fe | 103 | free(m->netif); |
bb1a05d6 | 104 | return mfree(m); |
9444b1f2 LP |
105 | } |
106 | ||
107 | int machine_save(Machine *m) { | |
108 | _cleanup_free_ char *temp_path = NULL; | |
109 | _cleanup_fclose_ FILE *f = NULL; | |
110 | int r; | |
111 | ||
112 | assert(m); | |
fbe55073 LP |
113 | |
114 | if (!m->state_file) | |
115 | return 0; | |
9444b1f2 LP |
116 | |
117 | if (!m->started) | |
118 | return 0; | |
119 | ||
37c1d5e9 | 120 | r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE); |
9444b1f2 | 121 | if (r < 0) |
dacd6cee | 122 | goto fail; |
9444b1f2 LP |
123 | |
124 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
125 | if (r < 0) | |
dacd6cee | 126 | goto fail; |
9444b1f2 | 127 | |
0d536673 | 128 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
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 | ||
c3350683 | 333 | static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 | 334 | assert(m); |
fbe55073 | 335 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 336 | |
89f7c846 | 337 | if (!m->unit) { |
354f62cf YW |
338 | _cleanup_free_ char *escaped = NULL, *scope = NULL; |
339 | char *description, *job = NULL; | |
340 | int r; | |
9444b1f2 | 341 | |
fb6becb4 | 342 | escaped = unit_name_escape(m->name); |
9444b1f2 LP |
343 | if (!escaped) |
344 | return log_oom(); | |
345 | ||
605405c6 | 346 | scope = strjoin("machine-", escaped, ".scope"); |
f526ab7e | 347 | if (!scope) |
9444b1f2 | 348 | return log_oom(); |
9444b1f2 | 349 | |
63c372cb | 350 | description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name); |
9444b1f2 | 351 | |
c3350683 | 352 | r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job); |
354f62cf YW |
353 | if (r < 0) |
354 | return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r)); | |
355 | ||
356 | m->unit = TAKE_PTR(scope); | |
357 | free_and_replace(m->scope_job, job); | |
9444b1f2 LP |
358 | } |
359 | ||
89f7c846 LP |
360 | if (m->unit) |
361 | hashmap_put(m->manager->machine_units, m->unit, m); | |
d0af76e6 | 362 | |
354f62cf | 363 | return 0; |
9444b1f2 LP |
364 | } |
365 | ||
c3350683 | 366 | int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 LP |
367 | int r; |
368 | ||
369 | assert(m); | |
370 | ||
fbe55073 LP |
371 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
372 | return -EOPNOTSUPP; | |
373 | ||
9444b1f2 LP |
374 | if (m->started) |
375 | return 0; | |
376 | ||
4a0b58c4 | 377 | r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb LP |
378 | if (r < 0) |
379 | return r; | |
380 | ||
fb6becb4 | 381 | /* Create cgroup */ |
c3350683 | 382 | r = machine_start_scope(m, properties, error); |
fb6becb4 LP |
383 | if (r < 0) |
384 | return r; | |
385 | ||
9444b1f2 | 386 | log_struct(LOG_INFO, |
2b044526 | 387 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR, |
9444b1f2 | 388 | "NAME=%s", m->name, |
de0671ee | 389 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 390 | LOG_MESSAGE("New machine %s.", m->name)); |
9444b1f2 | 391 | |
9444b1f2 LP |
392 | if (!dual_timestamp_is_set(&m->timestamp)) |
393 | dual_timestamp_get(&m->timestamp); | |
394 | ||
395 | m->started = true; | |
396 | ||
397 | /* Save new machine data */ | |
398 | machine_save(m); | |
399 | ||
400 | machine_send_signal(m, true); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
fb6becb4 | 405 | static int machine_stop_scope(Machine *m) { |
4afd3348 | 406 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
39883f62 | 407 | char *job = NULL; |
b92d0b4c | 408 | int r, q; |
9444b1f2 LP |
409 | |
410 | assert(m); | |
fbe55073 | 411 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 412 | |
89f7c846 | 413 | if (!m->unit) |
fb6becb4 | 414 | return 0; |
9444b1f2 | 415 | |
c00a4c8f | 416 | r = manager_stop_unit(m->manager, m->unit, &error, &job); |
b92d0b4c LP |
417 | if (r < 0) { |
418 | log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r)); | |
419 | sd_bus_error_free(&error); | |
420 | } else | |
421 | free_and_replace(m->scope_job, job); | |
422 | ||
423 | q = manager_unref_unit(m->manager, m->unit, &error); | |
424 | if (q < 0) | |
425 | log_warning_errno(q, "Failed to drop reference to machine scope, ignoring: %s", bus_error_message(&error, r)); | |
9444b1f2 | 426 | |
b92d0b4c | 427 | return r; |
9444b1f2 LP |
428 | } |
429 | ||
430 | int machine_stop(Machine *m) { | |
49f3fffd LP |
431 | int r; |
432 | assert(m); | |
433 | ||
fbe55073 LP |
434 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
435 | return -EOPNOTSUPP; | |
436 | ||
49f3fffd LP |
437 | r = machine_stop_scope(m); |
438 | ||
439 | m->stopping = true; | |
440 | ||
441 | machine_save(m); | |
442 | ||
443 | return r; | |
444 | } | |
445 | ||
446 | int machine_finalize(Machine *m) { | |
9444b1f2 LP |
447 | assert(m); |
448 | ||
449 | if (m->started) | |
450 | log_struct(LOG_INFO, | |
2b044526 | 451 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR, |
9444b1f2 | 452 | "NAME=%s", m->name, |
de0671ee | 453 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 454 | LOG_MESSAGE("Machine %s terminated.", m->name)); |
9444b1f2 | 455 | |
89f7c846 | 456 | machine_unlink(m); |
9444b1f2 LP |
457 | machine_add_to_gc_queue(m); |
458 | ||
49f3fffd | 459 | if (m->started) { |
9444b1f2 | 460 | machine_send_signal(m, false); |
49f3fffd LP |
461 | m->started = false; |
462 | } | |
9444b1f2 | 463 | |
49f3fffd | 464 | return 0; |
9444b1f2 LP |
465 | } |
466 | ||
554ce41f | 467 | bool machine_may_gc(Machine *m, bool drop_not_started) { |
9444b1f2 LP |
468 | assert(m); |
469 | ||
fbe55073 | 470 | if (m->class == MACHINE_HOST) |
554ce41f | 471 | return false; |
fbe55073 | 472 | |
9444b1f2 | 473 | if (drop_not_started && !m->started) |
554ce41f | 474 | return true; |
9444b1f2 | 475 | |
c3350683 | 476 | if (m->scope_job && manager_job_is_active(m->manager, m->scope_job)) |
554ce41f | 477 | return false; |
9444b1f2 | 478 | |
89f7c846 | 479 | if (m->unit && manager_unit_is_active(m->manager, m->unit)) |
554ce41f | 480 | return false; |
9444b1f2 | 481 | |
554ce41f | 482 | return true; |
9444b1f2 LP |
483 | } |
484 | ||
485 | void machine_add_to_gc_queue(Machine *m) { | |
486 | assert(m); | |
487 | ||
488 | if (m->in_gc_queue) | |
489 | return; | |
490 | ||
71fda00f | 491 | LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 LP |
492 | m->in_gc_queue = true; |
493 | } | |
494 | ||
fb6becb4 LP |
495 | MachineState machine_get_state(Machine *s) { |
496 | assert(s); | |
9444b1f2 | 497 | |
fbe55073 LP |
498 | if (s->class == MACHINE_HOST) |
499 | return MACHINE_RUNNING; | |
500 | ||
49f3fffd LP |
501 | if (s->stopping) |
502 | return MACHINE_CLOSING; | |
503 | ||
fb6becb4 | 504 | if (s->scope_job) |
49f3fffd | 505 | return MACHINE_OPENING; |
9444b1f2 | 506 | |
fb6becb4 LP |
507 | return MACHINE_RUNNING; |
508 | } | |
9444b1f2 | 509 | |
fb6becb4 LP |
510 | int machine_kill(Machine *m, KillWho who, int signo) { |
511 | assert(m); | |
9444b1f2 | 512 | |
fbe55073 LP |
513 | if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER)) |
514 | return -EOPNOTSUPP; | |
515 | ||
89f7c846 | 516 | if (!m->unit) |
fb6becb4 | 517 | return -ESRCH; |
9444b1f2 | 518 | |
de58a50e LP |
519 | if (who == KILL_LEADER) { |
520 | /* If we shall simply kill the leader, do so directly */ | |
521 | ||
522 | if (kill(m->leader, signo) < 0) | |
523 | return -errno; | |
9d685ca8 ED |
524 | |
525 | return 0; | |
de58a50e LP |
526 | } |
527 | ||
b938cb90 | 528 | /* Otherwise, make PID 1 do it for us, for the entire cgroup */ |
de58a50e | 529 | return manager_kill_unit(m->manager, m->unit, signo, NULL); |
9444b1f2 LP |
530 | } |
531 | ||
fbe55073 LP |
532 | int machine_openpt(Machine *m, int flags) { |
533 | assert(m); | |
534 | ||
535 | switch (m->class) { | |
536 | ||
5f430ff7 LP |
537 | case MACHINE_HOST: { |
538 | int fd; | |
539 | ||
540 | fd = posix_openpt(flags); | |
541 | if (fd < 0) | |
542 | return -errno; | |
543 | ||
544 | if (unlockpt(fd) < 0) | |
545 | return -errno; | |
546 | ||
547 | return fd; | |
548 | } | |
fbe55073 LP |
549 | |
550 | case MACHINE_CONTAINER: | |
551 | if (m->leader <= 0) | |
552 | return -EINVAL; | |
553 | ||
554 | return openpt_in_namespace(m->leader, flags); | |
555 | ||
556 | default: | |
557 | return -EOPNOTSUPP; | |
558 | } | |
559 | } | |
560 | ||
40e1f4ea LP |
561 | int machine_open_terminal(Machine *m, const char *path, int mode) { |
562 | assert(m); | |
563 | ||
564 | switch (m->class) { | |
565 | ||
566 | case MACHINE_HOST: | |
567 | return open_terminal(path, mode); | |
568 | ||
569 | case MACHINE_CONTAINER: | |
570 | if (m->leader <= 0) | |
571 | return -EINVAL; | |
572 | ||
573 | return open_terminal_in_namespace(m->leader, path, mode); | |
574 | ||
575 | default: | |
576 | return -EOPNOTSUPP; | |
577 | } | |
578 | } | |
579 | ||
9b420b3c LP |
580 | void machine_release_unit(Machine *m) { |
581 | assert(m); | |
582 | ||
583 | if (!m->unit) | |
584 | return; | |
585 | ||
586 | (void) hashmap_remove(m->manager->machine_units, m->unit); | |
a1e58e8e | 587 | m->unit = mfree(m->unit); |
9b420b3c LP |
588 | } |
589 | ||
3401419b | 590 | int machine_get_uid_shift(Machine *m, uid_t *ret) { |
fbd0b64f | 591 | char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
3401419b LP |
592 | uid_t uid_base, uid_shift, uid_range; |
593 | gid_t gid_base, gid_shift, gid_range; | |
594 | _cleanup_fclose_ FILE *f = NULL; | |
595 | int k; | |
596 | ||
597 | assert(m); | |
598 | assert(ret); | |
599 | ||
600 | /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple | |
601 | * mappings. In most cases setups should be simple like this, and administrators should only care about the | |
602 | * basic offset a container has relative to the host. This is what this function exposes. | |
603 | * | |
604 | * If we encounter any more complex mappings we politely refuse this with ENXIO. */ | |
605 | ||
606 | if (m->class == MACHINE_HOST) { | |
607 | *ret = 0; | |
608 | return 0; | |
609 | } | |
610 | ||
611 | if (m->class != MACHINE_CONTAINER) | |
612 | return -EOPNOTSUPP; | |
613 | ||
614 | xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader); | |
615 | f = fopen(p, "re"); | |
616 | if (!f) { | |
617 | if (errno == ENOENT) { | |
618 | /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */ | |
619 | *ret = 0; | |
620 | return 0; | |
621 | } | |
622 | ||
623 | return -errno; | |
624 | } | |
625 | ||
626 | /* Read the first line. There's at least one. */ | |
627 | errno = 0; | |
628 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range); | |
629 | if (k != 3) { | |
630 | if (ferror(f)) | |
631 | return -errno; | |
632 | ||
633 | return -EBADMSG; | |
634 | } | |
635 | ||
636 | /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */ | |
637 | if (uid_base != 0) | |
638 | return -ENXIO; | |
639 | /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */ | |
3a664727 | 640 | if (uid_range < UID_NOBODY) |
3401419b LP |
641 | return -ENXIO; |
642 | ||
643 | /* If there's more than one line, then we don't support this mapping. */ | |
644 | if (fgetc(f) != EOF) | |
645 | return -ENXIO; | |
646 | ||
647 | fclose(f); | |
648 | ||
649 | xsprintf(p, "/proc/" PID_FMT "/gid_map", m->leader); | |
650 | f = fopen(p, "re"); | |
651 | if (!f) | |
652 | return -errno; | |
653 | ||
654 | /* Read the first line. There's at least one. */ | |
655 | errno = 0; | |
656 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range); | |
657 | if (k != 3) { | |
658 | if (ferror(f)) | |
659 | return -errno; | |
660 | ||
661 | return -EBADMSG; | |
662 | } | |
663 | ||
664 | /* If there's more than one line, then we don't support this file. */ | |
665 | if (fgetc(f) != EOF) | |
666 | return -ENXIO; | |
667 | ||
668 | /* If the UID and GID mapping doesn't match, we don't support this mapping. */ | |
669 | if (uid_base != (uid_t) gid_base) | |
670 | return -ENXIO; | |
671 | if (uid_shift != (uid_t) gid_shift) | |
672 | return -ENXIO; | |
673 | if (uid_range != (uid_t) gid_range) | |
674 | return -ENXIO; | |
675 | ||
676 | *ret = uid_shift; | |
677 | return 0; | |
678 | } | |
679 | ||
9444b1f2 LP |
680 | static const char* const machine_class_table[_MACHINE_CLASS_MAX] = { |
681 | [MACHINE_CONTAINER] = "container", | |
fbe55073 LP |
682 | [MACHINE_VM] = "vm", |
683 | [MACHINE_HOST] = "host", | |
9444b1f2 LP |
684 | }; |
685 | ||
686 | DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass); | |
fb6becb4 LP |
687 | |
688 | static const char* const machine_state_table[_MACHINE_STATE_MAX] = { | |
689 | [MACHINE_OPENING] = "opening", | |
690 | [MACHINE_RUNNING] = "running", | |
691 | [MACHINE_CLOSING] = "closing" | |
692 | }; | |
693 | ||
694 | DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState); | |
1ee306e1 LP |
695 | |
696 | static const char* const kill_who_table[_KILL_WHO_MAX] = { | |
697 | [KILL_LEADER] = "leader", | |
698 | [KILL_ALL] = "all" | |
699 | }; | |
700 | ||
701 | DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho); |