]>
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 | ||
1a5a177e | 254 | r = parse_env_file(NULL, m->state_file, NEWLINE, |
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, | |
9b5ed6fe | 264 | "NETIF", &netif, |
9444b1f2 LP |
265 | NULL); |
266 | if (r < 0) { | |
267 | if (r == -ENOENT) | |
268 | return 0; | |
269 | ||
8d3d7072 | 270 | return log_error_errno(r, "Failed to read %s: %m", m->state_file); |
9444b1f2 LP |
271 | } |
272 | ||
273 | if (id) | |
274 | sd_id128_from_string(id, &m->id); | |
275 | ||
276 | if (leader) | |
277 | parse_pid(leader, &m->leader); | |
278 | ||
279 | if (class) { | |
280 | MachineClass c; | |
281 | ||
282 | c = machine_class_from_string(class); | |
283 | if (c >= 0) | |
284 | m->class = c; | |
285 | } | |
286 | ||
b895a735 | 287 | if (realtime) |
d68c645b | 288 | (void) deserialize_usec(realtime, &m->timestamp.realtime); |
b895a735 | 289 | if (monotonic) |
d68c645b | 290 | (void) deserialize_usec(monotonic, &m->timestamp.monotonic); |
9444b1f2 | 291 | |
9b5ed6fe | 292 | if (netif) { |
75a8fd6a SS |
293 | size_t allocated = 0, nr = 0; |
294 | const char *p; | |
9b5ed6fe LP |
295 | int *ni = NULL; |
296 | ||
75a8fd6a | 297 | p = netif; |
9ed794a3 | 298 | for (;;) { |
75a8fd6a | 299 | _cleanup_free_ char *word = NULL; |
9b5ed6fe LP |
300 | int ifi; |
301 | ||
75a8fd6a | 302 | r = extract_first_word(&p, &word, NULL, 0); |
75a8fd6a SS |
303 | if (r == 0) |
304 | break; | |
6a37c684 | 305 | if (r == -ENOMEM) |
52278ad3 | 306 | return log_oom(); |
6a37c684 | 307 | if (r < 0) { |
52278ad3 | 308 | log_warning_errno(r, "Failed to parse NETIF: %s", netif); |
6a37c684 | 309 | break; |
52278ad3 | 310 | } |
75a8fd6a | 311 | |
6ad623a3 | 312 | if (parse_ifindex(word, &ifi) < 0) |
9b5ed6fe LP |
313 | continue; |
314 | ||
315 | if (!GREEDY_REALLOC(ni, allocated, nr+1)) { | |
316 | free(ni); | |
317 | return log_oom(); | |
318 | } | |
319 | ||
320 | ni[nr++] = ifi; | |
321 | } | |
322 | ||
323 | free(m->netif); | |
324 | m->netif = ni; | |
325 | m->n_netif = nr; | |
326 | } | |
327 | ||
9444b1f2 LP |
328 | return r; |
329 | } | |
330 | ||
c3350683 | 331 | static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 | 332 | assert(m); |
fbe55073 | 333 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 334 | |
89f7c846 | 335 | if (!m->unit) { |
354f62cf YW |
336 | _cleanup_free_ char *escaped = NULL, *scope = NULL; |
337 | char *description, *job = NULL; | |
338 | int r; | |
9444b1f2 | 339 | |
fb6becb4 | 340 | escaped = unit_name_escape(m->name); |
9444b1f2 LP |
341 | if (!escaped) |
342 | return log_oom(); | |
343 | ||
605405c6 | 344 | scope = strjoin("machine-", escaped, ".scope"); |
f526ab7e | 345 | if (!scope) |
9444b1f2 | 346 | return log_oom(); |
9444b1f2 | 347 | |
63c372cb | 348 | description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name); |
9444b1f2 | 349 | |
c3350683 | 350 | r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job); |
354f62cf YW |
351 | if (r < 0) |
352 | return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r)); | |
353 | ||
354 | m->unit = TAKE_PTR(scope); | |
355 | free_and_replace(m->scope_job, job); | |
9444b1f2 LP |
356 | } |
357 | ||
89f7c846 LP |
358 | if (m->unit) |
359 | hashmap_put(m->manager->machine_units, m->unit, m); | |
d0af76e6 | 360 | |
354f62cf | 361 | return 0; |
9444b1f2 LP |
362 | } |
363 | ||
c3350683 | 364 | int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) { |
9444b1f2 LP |
365 | int r; |
366 | ||
367 | assert(m); | |
368 | ||
fbe55073 LP |
369 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
370 | return -EOPNOTSUPP; | |
371 | ||
9444b1f2 LP |
372 | if (m->started) |
373 | return 0; | |
374 | ||
4a0b58c4 | 375 | r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m); |
d3e84ddb LP |
376 | if (r < 0) |
377 | return r; | |
378 | ||
fb6becb4 | 379 | /* Create cgroup */ |
c3350683 | 380 | r = machine_start_scope(m, properties, error); |
fb6becb4 LP |
381 | if (r < 0) |
382 | return r; | |
383 | ||
9444b1f2 | 384 | log_struct(LOG_INFO, |
2b044526 | 385 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR, |
9444b1f2 | 386 | "NAME=%s", m->name, |
de0671ee | 387 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 388 | LOG_MESSAGE("New machine %s.", m->name)); |
9444b1f2 | 389 | |
9444b1f2 LP |
390 | if (!dual_timestamp_is_set(&m->timestamp)) |
391 | dual_timestamp_get(&m->timestamp); | |
392 | ||
393 | m->started = true; | |
394 | ||
395 | /* Save new machine data */ | |
396 | machine_save(m); | |
397 | ||
398 | machine_send_signal(m, true); | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
fb6becb4 | 403 | static int machine_stop_scope(Machine *m) { |
4afd3348 | 404 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
39883f62 | 405 | char *job = NULL; |
b92d0b4c | 406 | int r, q; |
9444b1f2 LP |
407 | |
408 | assert(m); | |
fbe55073 | 409 | assert(m->class != MACHINE_HOST); |
9444b1f2 | 410 | |
89f7c846 | 411 | if (!m->unit) |
fb6becb4 | 412 | return 0; |
9444b1f2 | 413 | |
c00a4c8f | 414 | r = manager_stop_unit(m->manager, m->unit, &error, &job); |
b92d0b4c LP |
415 | if (r < 0) { |
416 | log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r)); | |
417 | sd_bus_error_free(&error); | |
418 | } else | |
419 | free_and_replace(m->scope_job, job); | |
420 | ||
421 | q = manager_unref_unit(m->manager, m->unit, &error); | |
422 | if (q < 0) | |
423 | log_warning_errno(q, "Failed to drop reference to machine scope, ignoring: %s", bus_error_message(&error, r)); | |
9444b1f2 | 424 | |
b92d0b4c | 425 | return r; |
9444b1f2 LP |
426 | } |
427 | ||
428 | int machine_stop(Machine *m) { | |
49f3fffd LP |
429 | int r; |
430 | assert(m); | |
431 | ||
fbe55073 LP |
432 | if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM)) |
433 | return -EOPNOTSUPP; | |
434 | ||
49f3fffd LP |
435 | r = machine_stop_scope(m); |
436 | ||
437 | m->stopping = true; | |
438 | ||
439 | machine_save(m); | |
440 | ||
441 | return r; | |
442 | } | |
443 | ||
444 | int machine_finalize(Machine *m) { | |
9444b1f2 LP |
445 | assert(m); |
446 | ||
447 | if (m->started) | |
448 | log_struct(LOG_INFO, | |
2b044526 | 449 | "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR, |
9444b1f2 | 450 | "NAME=%s", m->name, |
de0671ee | 451 | "LEADER="PID_FMT, m->leader, |
a1230ff9 | 452 | LOG_MESSAGE("Machine %s terminated.", m->name)); |
9444b1f2 | 453 | |
89f7c846 | 454 | machine_unlink(m); |
9444b1f2 LP |
455 | machine_add_to_gc_queue(m); |
456 | ||
49f3fffd | 457 | if (m->started) { |
9444b1f2 | 458 | machine_send_signal(m, false); |
49f3fffd LP |
459 | m->started = false; |
460 | } | |
9444b1f2 | 461 | |
49f3fffd | 462 | return 0; |
9444b1f2 LP |
463 | } |
464 | ||
554ce41f | 465 | bool machine_may_gc(Machine *m, bool drop_not_started) { |
9444b1f2 LP |
466 | assert(m); |
467 | ||
fbe55073 | 468 | if (m->class == MACHINE_HOST) |
554ce41f | 469 | return false; |
fbe55073 | 470 | |
9444b1f2 | 471 | if (drop_not_started && !m->started) |
554ce41f | 472 | return true; |
9444b1f2 | 473 | |
c3350683 | 474 | if (m->scope_job && manager_job_is_active(m->manager, m->scope_job)) |
554ce41f | 475 | return false; |
9444b1f2 | 476 | |
89f7c846 | 477 | if (m->unit && manager_unit_is_active(m->manager, m->unit)) |
554ce41f | 478 | return false; |
9444b1f2 | 479 | |
554ce41f | 480 | return true; |
9444b1f2 LP |
481 | } |
482 | ||
483 | void machine_add_to_gc_queue(Machine *m) { | |
484 | assert(m); | |
485 | ||
486 | if (m->in_gc_queue) | |
487 | return; | |
488 | ||
71fda00f | 489 | LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m); |
9444b1f2 LP |
490 | m->in_gc_queue = true; |
491 | } | |
492 | ||
fb6becb4 LP |
493 | MachineState machine_get_state(Machine *s) { |
494 | assert(s); | |
9444b1f2 | 495 | |
fbe55073 LP |
496 | if (s->class == MACHINE_HOST) |
497 | return MACHINE_RUNNING; | |
498 | ||
49f3fffd LP |
499 | if (s->stopping) |
500 | return MACHINE_CLOSING; | |
501 | ||
fb6becb4 | 502 | if (s->scope_job) |
49f3fffd | 503 | return MACHINE_OPENING; |
9444b1f2 | 504 | |
fb6becb4 LP |
505 | return MACHINE_RUNNING; |
506 | } | |
9444b1f2 | 507 | |
fb6becb4 LP |
508 | int machine_kill(Machine *m, KillWho who, int signo) { |
509 | assert(m); | |
9444b1f2 | 510 | |
fbe55073 LP |
511 | if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER)) |
512 | return -EOPNOTSUPP; | |
513 | ||
89f7c846 | 514 | if (!m->unit) |
fb6becb4 | 515 | return -ESRCH; |
9444b1f2 | 516 | |
de58a50e LP |
517 | if (who == KILL_LEADER) { |
518 | /* If we shall simply kill the leader, do so directly */ | |
519 | ||
520 | if (kill(m->leader, signo) < 0) | |
521 | return -errno; | |
9d685ca8 ED |
522 | |
523 | return 0; | |
de58a50e LP |
524 | } |
525 | ||
b938cb90 | 526 | /* Otherwise, make PID 1 do it for us, for the entire cgroup */ |
de58a50e | 527 | return manager_kill_unit(m->manager, m->unit, signo, NULL); |
9444b1f2 LP |
528 | } |
529 | ||
fbe55073 LP |
530 | int machine_openpt(Machine *m, int flags) { |
531 | assert(m); | |
532 | ||
533 | switch (m->class) { | |
534 | ||
5f430ff7 LP |
535 | case MACHINE_HOST: { |
536 | int fd; | |
537 | ||
538 | fd = posix_openpt(flags); | |
539 | if (fd < 0) | |
540 | return -errno; | |
541 | ||
542 | if (unlockpt(fd) < 0) | |
543 | return -errno; | |
544 | ||
545 | return fd; | |
546 | } | |
fbe55073 LP |
547 | |
548 | case MACHINE_CONTAINER: | |
549 | if (m->leader <= 0) | |
550 | return -EINVAL; | |
551 | ||
552 | return openpt_in_namespace(m->leader, flags); | |
553 | ||
554 | default: | |
555 | return -EOPNOTSUPP; | |
556 | } | |
557 | } | |
558 | ||
40e1f4ea LP |
559 | int machine_open_terminal(Machine *m, const char *path, int mode) { |
560 | assert(m); | |
561 | ||
562 | switch (m->class) { | |
563 | ||
564 | case MACHINE_HOST: | |
565 | return open_terminal(path, mode); | |
566 | ||
567 | case MACHINE_CONTAINER: | |
568 | if (m->leader <= 0) | |
569 | return -EINVAL; | |
570 | ||
571 | return open_terminal_in_namespace(m->leader, path, mode); | |
572 | ||
573 | default: | |
574 | return -EOPNOTSUPP; | |
575 | } | |
576 | } | |
577 | ||
9b420b3c LP |
578 | void machine_release_unit(Machine *m) { |
579 | assert(m); | |
580 | ||
581 | if (!m->unit) | |
582 | return; | |
583 | ||
584 | (void) hashmap_remove(m->manager->machine_units, m->unit); | |
a1e58e8e | 585 | m->unit = mfree(m->unit); |
9b420b3c LP |
586 | } |
587 | ||
3401419b | 588 | int machine_get_uid_shift(Machine *m, uid_t *ret) { |
fbd0b64f | 589 | char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
3401419b LP |
590 | uid_t uid_base, uid_shift, uid_range; |
591 | gid_t gid_base, gid_shift, gid_range; | |
592 | _cleanup_fclose_ FILE *f = NULL; | |
593 | int k; | |
594 | ||
595 | assert(m); | |
596 | assert(ret); | |
597 | ||
598 | /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple | |
599 | * mappings. In most cases setups should be simple like this, and administrators should only care about the | |
600 | * basic offset a container has relative to the host. This is what this function exposes. | |
601 | * | |
602 | * If we encounter any more complex mappings we politely refuse this with ENXIO. */ | |
603 | ||
604 | if (m->class == MACHINE_HOST) { | |
605 | *ret = 0; | |
606 | return 0; | |
607 | } | |
608 | ||
609 | if (m->class != MACHINE_CONTAINER) | |
610 | return -EOPNOTSUPP; | |
611 | ||
612 | xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader); | |
613 | f = fopen(p, "re"); | |
614 | if (!f) { | |
615 | if (errno == ENOENT) { | |
616 | /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */ | |
617 | *ret = 0; | |
618 | return 0; | |
619 | } | |
620 | ||
621 | return -errno; | |
622 | } | |
623 | ||
624 | /* Read the first line. There's at least one. */ | |
625 | errno = 0; | |
626 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range); | |
627 | if (k != 3) { | |
628 | if (ferror(f)) | |
629 | return -errno; | |
630 | ||
631 | return -EBADMSG; | |
632 | } | |
633 | ||
634 | /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */ | |
635 | if (uid_base != 0) | |
636 | return -ENXIO; | |
637 | /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */ | |
3a664727 | 638 | if (uid_range < UID_NOBODY) |
3401419b LP |
639 | return -ENXIO; |
640 | ||
641 | /* If there's more than one line, then we don't support this mapping. */ | |
642 | if (fgetc(f) != EOF) | |
643 | return -ENXIO; | |
644 | ||
645 | fclose(f); | |
646 | ||
647 | xsprintf(p, "/proc/" PID_FMT "/gid_map", m->leader); | |
648 | f = fopen(p, "re"); | |
649 | if (!f) | |
650 | return -errno; | |
651 | ||
652 | /* Read the first line. There's at least one. */ | |
653 | errno = 0; | |
654 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range); | |
655 | if (k != 3) { | |
656 | if (ferror(f)) | |
657 | return -errno; | |
658 | ||
659 | return -EBADMSG; | |
660 | } | |
661 | ||
662 | /* If there's more than one line, then we don't support this file. */ | |
663 | if (fgetc(f) != EOF) | |
664 | return -ENXIO; | |
665 | ||
666 | /* If the UID and GID mapping doesn't match, we don't support this mapping. */ | |
667 | if (uid_base != (uid_t) gid_base) | |
668 | return -ENXIO; | |
669 | if (uid_shift != (uid_t) gid_shift) | |
670 | return -ENXIO; | |
671 | if (uid_range != (uid_t) gid_range) | |
672 | return -ENXIO; | |
673 | ||
674 | *ret = uid_shift; | |
675 | return 0; | |
676 | } | |
677 | ||
9444b1f2 LP |
678 | static const char* const machine_class_table[_MACHINE_CLASS_MAX] = { |
679 | [MACHINE_CONTAINER] = "container", | |
fbe55073 LP |
680 | [MACHINE_VM] = "vm", |
681 | [MACHINE_HOST] = "host", | |
9444b1f2 LP |
682 | }; |
683 | ||
684 | DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass); | |
fb6becb4 LP |
685 | |
686 | static const char* const machine_state_table[_MACHINE_STATE_MAX] = { | |
687 | [MACHINE_OPENING] = "opening", | |
688 | [MACHINE_RUNNING] = "running", | |
689 | [MACHINE_CLOSING] = "closing" | |
690 | }; | |
691 | ||
692 | DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState); | |
1ee306e1 LP |
693 | |
694 | static const char* const kill_who_table[_KILL_WHO_MAX] = { | |
695 | [KILL_LEADER] = "leader", | |
696 | [KILL_ALL] = "all" | |
697 | }; | |
698 | ||
699 | DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho); |