]>
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" | |
4f5dd394 | 13 | #include "escape.h" |
cf0fbc49 | 14 | #include "extract-word.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
9444b1f2 | 16 | #include "fileio.h" |
f97b34a6 | 17 | #include "format-util.h" |
66cb2fde | 18 | #include "hashmap.h" |
4f5dd394 | 19 | #include "machine-dbus.h" |
3ffd4af2 | 20 | #include "machine.h" |
66cb2fde | 21 | #include "mkdir.h" |
6bedfcbb | 22 | #include "parse-util.h" |
4a0b58c4 | 23 | #include "process-util.h" |
d68c645b | 24 | #include "serialize.h" |
9444b1f2 | 25 | #include "special.h" |
3401419b | 26 | #include "stdio-util.h" |
8b43440b | 27 | #include "string-table.h" |
66cb2fde | 28 | #include "terminal-util.h" |
fb6becb4 | 29 | #include "unit-name.h" |
3a664727 | 30 | #include "user-util.h" |
66cb2fde | 31 | #include "util.h" |
9444b1f2 | 32 | |
fbe55073 | 33 | Machine* machine_new(Manager *manager, MachineClass class, const char *name) { |
9444b1f2 LP |
34 | Machine *m; |
35 | ||
36 | assert(manager); | |
fbe55073 | 37 | assert(class < _MACHINE_CLASS_MAX); |
9444b1f2 LP |
38 | assert(name); |
39 | ||
fbe55073 LP |
40 | /* Passing class == _MACHINE_CLASS_INVALID here is fine. It |
41 | * means as much as "we don't know yet", and that we'll figure | |
42 | * it out later when loading the state file. */ | |
43 | ||
9444b1f2 LP |
44 | m = new0(Machine, 1); |
45 | if (!m) | |
46 | return NULL; | |
47 | ||
48 | m->name = strdup(name); | |
49 | if (!m->name) | |
50 | goto fail; | |
51 | ||
fbe55073 LP |
52 | if (class != MACHINE_HOST) { |
53 | m->state_file = strappend("/run/systemd/machines/", m->name); | |
54 | if (!m->state_file) | |
55 | goto fail; | |
56 | } | |
57 | ||
58 | m->class = class; | |
9444b1f2 LP |
59 | |
60 | if (hashmap_put(manager->machines, m->name, m) < 0) | |
61 | goto fail; | |
62 | ||
9444b1f2 LP |
63 | m->manager = manager; |
64 | ||
65 | return m; | |
66 | ||
67 | fail: | |
68 | free(m->state_file); | |
69 | free(m->name); | |
6b430fdb | 70 | return mfree(m); |
9444b1f2 LP |
71 | } |
72 | ||
73 | void machine_free(Machine *m) { | |
74 | assert(m); | |
75 | ||
0370612e | 76 | while (m->operations) |
795c5d31 | 77 | operation_free(m->operations); |
0370612e | 78 | |
9444b1f2 | 79 | if (m->in_gc_queue) |
71fda00f | 80 | LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 | 81 | |
9b420b3c | 82 | machine_release_unit(m); |
9444b1f2 | 83 | |
fb6becb4 LP |
84 | free(m->scope_job); |
85 | ||
9b420b3c | 86 | (void) hashmap_remove(m->manager->machines, m->name); |
9444b1f2 | 87 | |
fbe55073 LP |
88 | if (m->manager->host_machine == m) |
89 | m->manager->host_machine = NULL; | |
90 | ||
d3e84ddb | 91 | if (m->leader > 0) |
4a0b58c4 | 92 | (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb | 93 | |
c3350683 | 94 | sd_bus_message_unref(m->create_message); |
fb6becb4 | 95 | |
9444b1f2 LP |
96 | free(m->name); |
97 | free(m->state_file); | |
98 | free(m->service); | |
9444b1f2 | 99 | free(m->root_directory); |
9b5ed6fe | 100 | free(m->netif); |
9444b1f2 LP |
101 | free(m); |
102 | } | |
103 | ||
104 | int machine_save(Machine *m) { | |
105 | _cleanup_free_ char *temp_path = NULL; | |
106 | _cleanup_fclose_ FILE *f = NULL; | |
107 | int r; | |
108 | ||
109 | assert(m); | |
fbe55073 LP |
110 | |
111 | if (!m->state_file) | |
112 | return 0; | |
9444b1f2 LP |
113 | |
114 | if (!m->started) | |
115 | return 0; | |
116 | ||
37c1d5e9 | 117 | r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE); |
9444b1f2 | 118 | if (r < 0) |
dacd6cee | 119 | goto fail; |
9444b1f2 LP |
120 | |
121 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
122 | if (r < 0) | |
dacd6cee | 123 | goto fail; |
9444b1f2 | 124 | |
0d536673 | 125 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
dacd6cee | 126 | (void) fchmod(fileno(f), 0644); |
9444b1f2 LP |
127 | |
128 | fprintf(f, | |
129 | "# This is private data. Do not parse.\n" | |
130 | "NAME=%s\n", | |
131 | m->name); | |
132 | ||
ca5405bb LP |
133 | if (m->unit) { |
134 | _cleanup_free_ char *escaped; | |
135 | ||
136 | escaped = cescape(m->unit); | |
137 | if (!escaped) { | |
138 | r = -ENOMEM; | |
dacd6cee | 139 | goto fail; |
ca5405bb LP |
140 | } |
141 | ||
142 | 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 */ | |
143 | } | |
fb6becb4 LP |
144 | |
145 | if (m->scope_job) | |
146 | fprintf(f, "SCOPE_JOB=%s\n", m->scope_job); | |
9444b1f2 | 147 | |
ca5405bb LP |
148 | if (m->service) { |
149 | _cleanup_free_ char *escaped; | |
9444b1f2 | 150 | |
ca5405bb LP |
151 | escaped = cescape(m->service); |
152 | if (!escaped) { | |
153 | r = -ENOMEM; | |
dacd6cee | 154 | goto fail; |
ca5405bb LP |
155 | } |
156 | fprintf(f, "SERVICE=%s\n", escaped); | |
157 | } | |
158 | ||
159 | if (m->root_directory) { | |
160 | _cleanup_free_ char *escaped; | |
161 | ||
162 | escaped = cescape(m->root_directory); | |
163 | if (!escaped) { | |
164 | r = -ENOMEM; | |
dacd6cee | 165 | goto fail; |
ca5405bb LP |
166 | } |
167 | fprintf(f, "ROOT=%s\n", escaped); | |
168 | } | |
9444b1f2 | 169 | |
3bbaff3e | 170 | if (!sd_id128_is_null(m->id)) |
9444b1f2 LP |
171 | fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id)); |
172 | ||
173 | if (m->leader != 0) | |
90b2de37 | 174 | fprintf(f, "LEADER="PID_FMT"\n", m->leader); |
9444b1f2 LP |
175 | |
176 | if (m->class != _MACHINE_CLASS_INVALID) | |
177 | fprintf(f, "CLASS=%s\n", machine_class_to_string(m->class)); | |
178 | ||
179 | if (dual_timestamp_is_set(&m->timestamp)) | |
180 | fprintf(f, | |
90b2de37 ZJS |
181 | "REALTIME="USEC_FMT"\n" |
182 | "MONOTONIC="USEC_FMT"\n", | |
183 | m->timestamp.realtime, | |
184 | m->timestamp.monotonic); | |
9444b1f2 | 185 | |
9b5ed6fe | 186 | if (m->n_netif > 0) { |
68e16e9c | 187 | size_t i; |
9b5ed6fe | 188 | |
0d536673 | 189 | fputs("NETIF=", f); |
9b5ed6fe LP |
190 | |
191 | for (i = 0; i < m->n_netif; i++) { | |
192 | if (i != 0) | |
0d536673 | 193 | fputc(' ', f); |
9b5ed6fe LP |
194 | |
195 | fprintf(f, "%i", m->netif[i]); | |
196 | } | |
197 | ||
0d536673 | 198 | fputc('\n', f); |
9b5ed6fe LP |
199 | } |
200 | ||
034753ac LP |
201 | r = fflush_and_check(f); |
202 | if (r < 0) | |
dacd6cee | 203 | goto fail; |
9444b1f2 | 204 | |
034753ac | 205 | if (rename(temp_path, m->state_file) < 0) { |
9444b1f2 | 206 | r = -errno; |
dacd6cee | 207 | goto fail; |
9444b1f2 LP |
208 | } |
209 | ||
89f7c846 LP |
210 | if (m->unit) { |
211 | char *sl; | |
212 | ||
213 | /* Create a symlink from the unit name to the machine | |
214 | * name, so that we can quickly find the machine for | |
e62d9b81 | 215 | * each given unit. Ignore error. */ |
63c372cb | 216 | sl = strjoina("/run/systemd/machines/unit:", m->unit); |
e62d9b81 | 217 | (void) symlink(m->name, sl); |
89f7c846 LP |
218 | } |
219 | ||
dacd6cee | 220 | return 0; |
034753ac | 221 | |
dacd6cee LP |
222 | fail: |
223 | (void) unlink(m->state_file); | |
224 | ||
225 | if (temp_path) | |
226 | (void) unlink(temp_path); | |
9444b1f2 | 227 | |
dacd6cee | 228 | return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file); |
9444b1f2 LP |
229 | } |
230 | ||
89f7c846 LP |
231 | static void machine_unlink(Machine *m) { |
232 | assert(m); | |
233 | ||
234 | if (m->unit) { | |
89f7c846 LP |
235 | char *sl; |
236 | ||
63c372cb | 237 | sl = strjoina("/run/systemd/machines/unit:", m->unit); |
491ac9f2 | 238 | (void) unlink(sl); |
89f7c846 LP |
239 | } |
240 | ||
241 | if (m->state_file) | |
491ac9f2 | 242 | (void) unlink(m->state_file); |
89f7c846 LP |
243 | } |
244 | ||
9444b1f2 | 245 | int machine_load(Machine *m) { |
9b5ed6fe | 246 | _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL; |
9444b1f2 LP |
247 | int r; |
248 | ||
249 | assert(m); | |
250 | ||
fbe55073 LP |
251 | if (!m->state_file) |
252 | return 0; | |
253 | ||
aa8fbc74 | 254 | r = parse_env_file(NULL, m->state_file, |
89f7c846 | 255 | "SCOPE", &m->unit, |
fb6becb4 | 256 | "SCOPE_JOB", &m->scope_job, |
9444b1f2 | 257 | "SERVICE", &m->service, |
9444b1f2 LP |
258 | "ROOT", &m->root_directory, |
259 | "ID", &id, | |
260 | "LEADER", &leader, | |
261 | "CLASS", &class, | |
262 | "REALTIME", &realtime, | |
263 | "MONOTONIC", &monotonic, | |
13df9c39 | 264 | "NETIF", &netif); |
9444b1f2 LP |
265 | if (r < 0) { |
266 | if (r == -ENOENT) | |
267 | return 0; | |
268 | ||
8d3d7072 | 269 | return log_error_errno(r, "Failed to read %s: %m", m->state_file); |
9444b1f2 LP |
270 | } |
271 | ||
272 | if (id) | |
273 | sd_id128_from_string(id, &m->id); | |
274 | ||
275 | if (leader) | |
276 | parse_pid(leader, &m->leader); | |
277 | ||
278 | if (class) { | |
279 | MachineClass c; | |
280 | ||
281 | c = machine_class_from_string(class); | |
282 | if (c >= 0) | |
283 | m->class = c; | |
284 | } | |
285 | ||
b895a735 | 286 | if (realtime) |
d68c645b | 287 | (void) deserialize_usec(realtime, &m->timestamp.realtime); |
b895a735 | 288 | if (monotonic) |
d68c645b | 289 | (void) deserialize_usec(monotonic, &m->timestamp.monotonic); |
9444b1f2 | 290 | |
9b5ed6fe | 291 | if (netif) { |
75a8fd6a SS |
292 | size_t allocated = 0, nr = 0; |
293 | const char *p; | |
9b5ed6fe LP |
294 | int *ni = NULL; |
295 | ||
75a8fd6a | 296 | p = netif; |
9ed794a3 | 297 | for (;;) { |
75a8fd6a | 298 | _cleanup_free_ char *word = NULL; |
9b5ed6fe LP |
299 | int ifi; |
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 | |
6ad623a3 | 311 | if (parse_ifindex(word, &ifi) < 0) |
9b5ed6fe LP |
312 | continue; |
313 | ||
314 | if (!GREEDY_REALLOC(ni, allocated, nr+1)) { | |
315 | free(ni); | |
316 | return log_oom(); | |
317 | } | |
318 | ||
319 | ni[nr++] = ifi; | |
320 | } | |
321 | ||
322 | free(m->netif); | |
323 | m->netif = ni; | |
324 | m->n_netif = nr; | |
325 | } | |
326 | ||
9444b1f2 LP |
327 | return r; |
328 | } | |
329 | ||
c3350683 | 330 | static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 | 331 | assert(m); |
fbe55073 | 332 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 333 | |
89f7c846 | 334 | if (!m->unit) { |
354f62cf YW |
335 | _cleanup_free_ char *escaped = NULL, *scope = NULL; |
336 | char *description, *job = NULL; | |
337 | int r; | |
9444b1f2 | 338 | |
fb6becb4 | 339 | escaped = unit_name_escape(m->name); |
9444b1f2 LP |
340 | if (!escaped) |
341 | return log_oom(); | |
342 | ||
605405c6 | 343 | scope = strjoin("machine-", escaped, ".scope"); |
f526ab7e | 344 | if (!scope) |
9444b1f2 | 345 | return log_oom(); |
9444b1f2 | 346 | |
63c372cb | 347 | description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name); |
9444b1f2 | 348 | |
c3350683 | 349 | r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job); |
354f62cf YW |
350 | if (r < 0) |
351 | return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r)); | |
352 | ||
353 | m->unit = TAKE_PTR(scope); | |
354 | free_and_replace(m->scope_job, job); | |
9444b1f2 LP |
355 | } |
356 | ||
89f7c846 LP |
357 | if (m->unit) |
358 | hashmap_put(m->manager->machine_units, m->unit, m); | |
d0af76e6 | 359 | |
354f62cf | 360 | return 0; |
9444b1f2 LP |
361 | } |
362 | ||
c3350683 | 363 | int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 LP |
364 | int r; |
365 | ||
366 | assert(m); | |
367 | ||
fbe55073 LP |
368 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
369 | return -EOPNOTSUPP; | |
370 | ||
9444b1f2 LP |
371 | if (m->started) |
372 | return 0; | |
373 | ||
4a0b58c4 | 374 | r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb LP |
375 | if (r < 0) |
376 | return r; | |
377 | ||
fb6becb4 | 378 | /* Create cgroup */ |
c3350683 | 379 | r = machine_start_scope(m, properties, error); |
fb6becb4 LP |
380 | if (r < 0) |
381 | return r; | |
382 | ||
9444b1f2 | 383 | log_struct(LOG_INFO, |
2b044526 | 384 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR, |
9444b1f2 | 385 | "NAME=%s", m->name, |
de0671ee | 386 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 387 | LOG_MESSAGE("New machine %s.", m->name)); |
9444b1f2 | 388 | |
9444b1f2 LP |
389 | if (!dual_timestamp_is_set(&m->timestamp)) |
390 | dual_timestamp_get(&m->timestamp); | |
391 | ||
392 | m->started = true; | |
393 | ||
394 | /* Save new machine data */ | |
395 | machine_save(m); | |
396 | ||
397 | machine_send_signal(m, true); | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
fb6becb4 | 402 | static int machine_stop_scope(Machine *m) { |
4afd3348 | 403 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
39883f62 | 404 | char *job = NULL; |
b92d0b4c | 405 | int r, q; |
9444b1f2 LP |
406 | |
407 | assert(m); | |
fbe55073 | 408 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 409 | |
89f7c846 | 410 | if (!m->unit) |
fb6becb4 | 411 | return 0; |
9444b1f2 | 412 | |
c00a4c8f | 413 | r = manager_stop_unit(m->manager, m->unit, &error, &job); |
b92d0b4c LP |
414 | if (r < 0) { |
415 | log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r)); | |
416 | sd_bus_error_free(&error); | |
417 | } else | |
418 | free_and_replace(m->scope_job, job); | |
419 | ||
420 | q = manager_unref_unit(m->manager, m->unit, &error); | |
421 | if (q < 0) | |
422 | log_warning_errno(q, "Failed to drop reference to machine scope, ignoring: %s", bus_error_message(&error, r)); | |
9444b1f2 | 423 | |
b92d0b4c | 424 | return r; |
9444b1f2 LP |
425 | } |
426 | ||
427 | int machine_stop(Machine *m) { | |
49f3fffd LP |
428 | int r; |
429 | assert(m); | |
430 | ||
fbe55073 LP |
431 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
432 | return -EOPNOTSUPP; | |
433 | ||
49f3fffd LP |
434 | r = machine_stop_scope(m); |
435 | ||
436 | m->stopping = true; | |
437 | ||
438 | machine_save(m); | |
439 | ||
440 | return r; | |
441 | } | |
442 | ||
443 | int machine_finalize(Machine *m) { | |
9444b1f2 LP |
444 | assert(m); |
445 | ||
446 | if (m->started) | |
447 | log_struct(LOG_INFO, | |
2b044526 | 448 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR, |
9444b1f2 | 449 | "NAME=%s", m->name, |
de0671ee | 450 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 451 | LOG_MESSAGE("Machine %s terminated.", m->name)); |
9444b1f2 | 452 | |
89f7c846 | 453 | machine_unlink(m); |
9444b1f2 LP |
454 | machine_add_to_gc_queue(m); |
455 | ||
49f3fffd | 456 | if (m->started) { |
9444b1f2 | 457 | machine_send_signal(m, false); |
49f3fffd LP |
458 | m->started = false; |
459 | } | |
9444b1f2 | 460 | |
49f3fffd | 461 | return 0; |
9444b1f2 LP |
462 | } |
463 | ||
554ce41f | 464 | bool machine_may_gc(Machine *m, bool drop_not_started) { |
9444b1f2 LP |
465 | assert(m); |
466 | ||
fbe55073 | 467 | if (m->class == MACHINE_HOST) |
554ce41f | 468 | return false; |
fbe55073 | 469 | |
9444b1f2 | 470 | if (drop_not_started && !m->started) |
554ce41f | 471 | return true; |
9444b1f2 | 472 | |
c3350683 | 473 | if (m->scope_job && manager_job_is_active(m->manager, m->scope_job)) |
554ce41f | 474 | return false; |
9444b1f2 | 475 | |
89f7c846 | 476 | if (m->unit && manager_unit_is_active(m->manager, m->unit)) |
554ce41f | 477 | return false; |
9444b1f2 | 478 | |
554ce41f | 479 | return true; |
9444b1f2 LP |
480 | } |
481 | ||
482 | void machine_add_to_gc_queue(Machine *m) { | |
483 | assert(m); | |
484 | ||
485 | if (m->in_gc_queue) | |
486 | return; | |
487 | ||
71fda00f | 488 | LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 LP |
489 | m->in_gc_queue = true; |
490 | } | |
491 | ||
fb6becb4 LP |
492 | MachineState machine_get_state(Machine *s) { |
493 | assert(s); | |
9444b1f2 | 494 | |
fbe55073 LP |
495 | if (s->class == MACHINE_HOST) |
496 | return MACHINE_RUNNING; | |
497 | ||
49f3fffd LP |
498 | if (s->stopping) |
499 | return MACHINE_CLOSING; | |
500 | ||
fb6becb4 | 501 | if (s->scope_job) |
49f3fffd | 502 | return MACHINE_OPENING; |
9444b1f2 | 503 | |
fb6becb4 LP |
504 | return MACHINE_RUNNING; |
505 | } | |
9444b1f2 | 506 | |
fb6becb4 LP |
507 | int machine_kill(Machine *m, KillWho who, int signo) { |
508 | assert(m); | |
9444b1f2 | 509 | |
fbe55073 LP |
510 | if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER)) |
511 | return -EOPNOTSUPP; | |
512 | ||
89f7c846 | 513 | if (!m->unit) |
fb6becb4 | 514 | return -ESRCH; |
9444b1f2 | 515 | |
de58a50e LP |
516 | if (who == KILL_LEADER) { |
517 | /* If we shall simply kill the leader, do so directly */ | |
518 | ||
519 | if (kill(m->leader, signo) < 0) | |
520 | return -errno; | |
9d685ca8 ED |
521 | |
522 | return 0; | |
de58a50e LP |
523 | } |
524 | ||
b938cb90 | 525 | /* Otherwise, make PID 1 do it for us, for the entire cgroup */ |
de58a50e | 526 | return manager_kill_unit(m->manager, m->unit, signo, NULL); |
9444b1f2 LP |
527 | } |
528 | ||
fbe55073 LP |
529 | int machine_openpt(Machine *m, int flags) { |
530 | assert(m); | |
531 | ||
532 | switch (m->class) { | |
533 | ||
5f430ff7 LP |
534 | case MACHINE_HOST: { |
535 | int fd; | |
536 | ||
537 | fd = posix_openpt(flags); | |
538 | if (fd < 0) | |
539 | return -errno; | |
540 | ||
541 | if (unlockpt(fd) < 0) | |
542 | return -errno; | |
543 | ||
544 | return fd; | |
545 | } | |
fbe55073 LP |
546 | |
547 | case MACHINE_CONTAINER: | |
548 | if (m->leader <= 0) | |
549 | return -EINVAL; | |
550 | ||
551 | return openpt_in_namespace(m->leader, flags); | |
552 | ||
553 | default: | |
554 | return -EOPNOTSUPP; | |
555 | } | |
556 | } | |
557 | ||
40e1f4ea LP |
558 | int machine_open_terminal(Machine *m, const char *path, int mode) { |
559 | assert(m); | |
560 | ||
561 | switch (m->class) { | |
562 | ||
563 | case MACHINE_HOST: | |
564 | return open_terminal(path, mode); | |
565 | ||
566 | case MACHINE_CONTAINER: | |
567 | if (m->leader <= 0) | |
568 | return -EINVAL; | |
569 | ||
570 | return open_terminal_in_namespace(m->leader, path, mode); | |
571 | ||
572 | default: | |
573 | return -EOPNOTSUPP; | |
574 | } | |
575 | } | |
576 | ||
9b420b3c LP |
577 | void machine_release_unit(Machine *m) { |
578 | assert(m); | |
579 | ||
580 | if (!m->unit) | |
581 | return; | |
582 | ||
583 | (void) hashmap_remove(m->manager->machine_units, m->unit); | |
a1e58e8e | 584 | m->unit = mfree(m->unit); |
9b420b3c LP |
585 | } |
586 | ||
3401419b | 587 | int machine_get_uid_shift(Machine *m, uid_t *ret) { |
fbd0b64f | 588 | char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
3401419b LP |
589 | uid_t uid_base, uid_shift, uid_range; |
590 | gid_t gid_base, gid_shift, gid_range; | |
591 | _cleanup_fclose_ FILE *f = NULL; | |
592 | int k; | |
593 | ||
594 | assert(m); | |
595 | assert(ret); | |
596 | ||
597 | /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple | |
598 | * mappings. In most cases setups should be simple like this, and administrators should only care about the | |
599 | * basic offset a container has relative to the host. This is what this function exposes. | |
600 | * | |
601 | * If we encounter any more complex mappings we politely refuse this with ENXIO. */ | |
602 | ||
603 | if (m->class == MACHINE_HOST) { | |
604 | *ret = 0; | |
605 | return 0; | |
606 | } | |
607 | ||
608 | if (m->class != MACHINE_CONTAINER) | |
609 | return -EOPNOTSUPP; | |
610 | ||
611 | xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader); | |
612 | f = fopen(p, "re"); | |
613 | if (!f) { | |
614 | if (errno == ENOENT) { | |
615 | /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */ | |
616 | *ret = 0; | |
617 | return 0; | |
618 | } | |
619 | ||
620 | return -errno; | |
621 | } | |
622 | ||
623 | /* Read the first line. There's at least one. */ | |
624 | errno = 0; | |
625 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range); | |
626 | if (k != 3) { | |
627 | if (ferror(f)) | |
628 | return -errno; | |
629 | ||
630 | return -EBADMSG; | |
631 | } | |
632 | ||
633 | /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */ | |
634 | if (uid_base != 0) | |
635 | return -ENXIO; | |
636 | /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */ | |
3a664727 | 637 | if (uid_range < UID_NOBODY) |
3401419b LP |
638 | return -ENXIO; |
639 | ||
640 | /* If there's more than one line, then we don't support this mapping. */ | |
641 | if (fgetc(f) != EOF) | |
642 | return -ENXIO; | |
643 | ||
644 | fclose(f); | |
645 | ||
646 | xsprintf(p, "/proc/" PID_FMT "/gid_map", m->leader); | |
647 | f = fopen(p, "re"); | |
648 | if (!f) | |
649 | return -errno; | |
650 | ||
651 | /* Read the first line. There's at least one. */ | |
652 | errno = 0; | |
653 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range); | |
654 | if (k != 3) { | |
655 | if (ferror(f)) | |
656 | return -errno; | |
657 | ||
658 | return -EBADMSG; | |
659 | } | |
660 | ||
661 | /* If there's more than one line, then we don't support this file. */ | |
662 | if (fgetc(f) != EOF) | |
663 | return -ENXIO; | |
664 | ||
665 | /* If the UID and GID mapping doesn't match, we don't support this mapping. */ | |
666 | if (uid_base != (uid_t) gid_base) | |
667 | return -ENXIO; | |
668 | if (uid_shift != (uid_t) gid_shift) | |
669 | return -ENXIO; | |
670 | if (uid_range != (uid_t) gid_range) | |
671 | return -ENXIO; | |
672 | ||
673 | *ret = uid_shift; | |
674 | return 0; | |
675 | } | |
676 | ||
9444b1f2 LP |
677 | static const char* const machine_class_table[_MACHINE_CLASS_MAX] = { |
678 | [MACHINE_CONTAINER] = "container", | |
fbe55073 LP |
679 | [MACHINE_VM] = "vm", |
680 | [MACHINE_HOST] = "host", | |
9444b1f2 LP |
681 | }; |
682 | ||
683 | DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass); | |
fb6becb4 LP |
684 | |
685 | static const char* const machine_state_table[_MACHINE_STATE_MAX] = { | |
686 | [MACHINE_OPENING] = "opening", | |
687 | [MACHINE_RUNNING] = "running", | |
688 | [MACHINE_CLOSING] = "closing" | |
689 | }; | |
690 | ||
691 | DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState); | |
1ee306e1 LP |
692 | |
693 | static const char* const kill_who_table[_KILL_WHO_MAX] = { | |
694 | [KILL_LEADER] = "leader", | |
695 | [KILL_ALL] = "all" | |
696 | }; | |
697 | ||
698 | DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho); |