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