]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machine.c
headers: remove unneeded includes from util.h
[thirdparty/systemd.git] / src / machine / machine.c
CommitLineData
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>
ca78ad1d 7#include <sys/stat.h>
9444b1f2 8
c3350683 9#include "sd-messages.h"
fb6becb4 10
b5efdb8a 11#include "alloc-util.h"
66cb2fde
LP
12#include "bus-error.h"
13#include "bus-util.h"
686d13b9 14#include "env-file.h"
4f5dd394 15#include "escape.h"
cf0fbc49 16#include "extract-word.h"
3ffd4af2 17#include "fd-util.h"
9444b1f2 18#include "fileio.h"
f97b34a6 19#include "format-util.h"
66cb2fde 20#include "hashmap.h"
4f5dd394 21#include "machine-dbus.h"
3ffd4af2 22#include "machine.h"
66cb2fde 23#include "mkdir.h"
6bedfcbb 24#include "parse-util.h"
4a0b58c4 25#include "process-util.h"
d68c645b 26#include "serialize.h"
9444b1f2 27#include "special.h"
3401419b 28#include "stdio-util.h"
8b43440b 29#include "string-table.h"
66cb2fde 30#include "terminal-util.h"
e4de7287 31#include "tmpfile-util.h"
fb6becb4 32#include "unit-name.h"
3a664727 33#include "user-util.h"
66cb2fde 34#include "util.h"
9444b1f2 35
fbe55073 36Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
9444b1f2
LP
37 Machine *m;
38
39 assert(manager);
fbe55073 40 assert(class < _MACHINE_CLASS_MAX);
9444b1f2
LP
41 assert(name);
42
fbe55073
LP
43 /* Passing class == _MACHINE_CLASS_INVALID here is fine. It
44 * means as much as "we don't know yet", and that we'll figure
45 * it out later when loading the state file. */
46
9444b1f2
LP
47 m = new0(Machine, 1);
48 if (!m)
49 return NULL;
50
51 m->name = strdup(name);
52 if (!m->name)
53 goto fail;
54
fbe55073
LP
55 if (class != MACHINE_HOST) {
56 m->state_file = strappend("/run/systemd/machines/", m->name);
57 if (!m->state_file)
58 goto fail;
59 }
60
61 m->class = class;
9444b1f2
LP
62
63 if (hashmap_put(manager->machines, m->name, m) < 0)
64 goto fail;
65
9444b1f2
LP
66 m->manager = manager;
67
68 return m;
69
70fail:
71 free(m->state_file);
72 free(m->name);
6b430fdb 73 return mfree(m);
9444b1f2
LP
74}
75
bb1a05d6
YW
76Machine* machine_free(Machine *m) {
77 if (!m)
78 return NULL;
9444b1f2 79
0370612e 80 while (m->operations)
795c5d31 81 operation_free(m->operations);
0370612e 82
9444b1f2 83 if (m->in_gc_queue)
71fda00f 84 LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m);
9444b1f2 85
9b420b3c 86 machine_release_unit(m);
9444b1f2 87
fb6becb4
LP
88 free(m->scope_job);
89
9b420b3c 90 (void) hashmap_remove(m->manager->machines, m->name);
9444b1f2 91
fbe55073
LP
92 if (m->manager->host_machine == m)
93 m->manager->host_machine = NULL;
94
d3e84ddb 95 if (m->leader > 0)
4a0b58c4 96 (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
d3e84ddb 97
c3350683 98 sd_bus_message_unref(m->create_message);
fb6becb4 99
9444b1f2
LP
100 free(m->name);
101 free(m->state_file);
102 free(m->service);
9444b1f2 103 free(m->root_directory);
9b5ed6fe 104 free(m->netif);
bb1a05d6 105 return mfree(m);
9444b1f2
LP
106}
107
108int 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 190 if (m->n_netif > 0) {
68e16e9c 191 size_t i;
9b5ed6fe 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
226fail:
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
235static 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 249int 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
aa8fbc74 258 r = parse_env_file(NULL, m->state_file,
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,
13df9c39 268 "NETIF", &netif);
9444b1f2
LP
269 if (r < 0) {
270 if (r == -ENOENT)
271 return 0;
272
8d3d7072 273 return log_error_errno(r, "Failed to read %s: %m", m->state_file);
9444b1f2
LP
274 }
275
276 if (id)
277 sd_id128_from_string(id, &m->id);
278
279 if (leader)
280 parse_pid(leader, &m->leader);
281
282 if (class) {
283 MachineClass c;
284
285 c = machine_class_from_string(class);
286 if (c >= 0)
287 m->class = c;
288 }
289
b895a735 290 if (realtime)
d68c645b 291 (void) deserialize_usec(realtime, &m->timestamp.realtime);
b895a735 292 if (monotonic)
d68c645b 293 (void) deserialize_usec(monotonic, &m->timestamp.monotonic);
9444b1f2 294
9b5ed6fe 295 if (netif) {
75a8fd6a
SS
296 size_t allocated = 0, nr = 0;
297 const char *p;
9b5ed6fe
LP
298 int *ni = NULL;
299
75a8fd6a 300 p = netif;
9ed794a3 301 for (;;) {
75a8fd6a 302 _cleanup_free_ char *word = NULL;
9b5ed6fe
LP
303 int ifi;
304
75a8fd6a 305 r = extract_first_word(&p, &word, NULL, 0);
75a8fd6a
SS
306 if (r == 0)
307 break;
6a37c684 308 if (r == -ENOMEM)
52278ad3 309 return log_oom();
6a37c684 310 if (r < 0) {
52278ad3 311 log_warning_errno(r, "Failed to parse NETIF: %s", netif);
6a37c684 312 break;
52278ad3 313 }
75a8fd6a 314
6ad623a3 315 if (parse_ifindex(word, &ifi) < 0)
9b5ed6fe
LP
316 continue;
317
318 if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
319 free(ni);
320 return log_oom();
321 }
322
323 ni[nr++] = ifi;
324 }
325
326 free(m->netif);
327 m->netif = ni;
328 m->n_netif = nr;
329 }
330
9444b1f2
LP
331 return r;
332}
333
c3350683 334static int machine_start_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
9444b1f2 335 assert(m);
fbe55073 336 assert(m->class != MACHINE_HOST);
9444b1f2 337
89f7c846 338 if (!m->unit) {
354f62cf
YW
339 _cleanup_free_ char *escaped = NULL, *scope = NULL;
340 char *description, *job = NULL;
341 int r;
9444b1f2 342
fb6becb4 343 escaped = unit_name_escape(m->name);
9444b1f2
LP
344 if (!escaped)
345 return log_oom();
346
605405c6 347 scope = strjoin("machine-", escaped, ".scope");
f526ab7e 348 if (!scope)
9444b1f2 349 return log_oom();
9444b1f2 350
63c372cb 351 description = strjoina(m->class == MACHINE_VM ? "Virtual Machine " : "Container ", m->name);
9444b1f2 352
c3350683 353 r = manager_start_scope(m->manager, scope, m->leader, SPECIAL_MACHINE_SLICE, description, properties, error, &job);
354f62cf
YW
354 if (r < 0)
355 return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r));
356
357 m->unit = TAKE_PTR(scope);
358 free_and_replace(m->scope_job, job);
9444b1f2
LP
359 }
360
89f7c846
LP
361 if (m->unit)
362 hashmap_put(m->manager->machine_units, m->unit, m);
d0af76e6 363
354f62cf 364 return 0;
9444b1f2
LP
365}
366
c3350683 367int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
9444b1f2
LP
368 int r;
369
370 assert(m);
371
fbe55073
LP
372 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
373 return -EOPNOTSUPP;
374
9444b1f2
LP
375 if (m->started)
376 return 0;
377
4a0b58c4 378 r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
d3e84ddb
LP
379 if (r < 0)
380 return r;
381
fb6becb4 382 /* Create cgroup */
c3350683 383 r = machine_start_scope(m, properties, error);
fb6becb4
LP
384 if (r < 0)
385 return r;
386
9444b1f2 387 log_struct(LOG_INFO,
2b044526 388 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR,
9444b1f2 389 "NAME=%s", m->name,
de0671ee 390 "LEADER="PID_FMT, m->leader,
a1230ff9 391 LOG_MESSAGE("New machine %s.", m->name));
9444b1f2 392
9444b1f2
LP
393 if (!dual_timestamp_is_set(&m->timestamp))
394 dual_timestamp_get(&m->timestamp);
395
396 m->started = true;
397
398 /* Save new machine data */
399 machine_save(m);
400
401 machine_send_signal(m, true);
9fdcbae5 402 (void) manager_enqueue_nscd_cache_flush(m->manager);
9444b1f2
LP
403
404 return 0;
405}
406
fb6becb4 407static int machine_stop_scope(Machine *m) {
4afd3348 408 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
39883f62 409 char *job = NULL;
b92d0b4c 410 int r, q;
9444b1f2
LP
411
412 assert(m);
fbe55073 413 assert(m->class != MACHINE_HOST);
9444b1f2 414
89f7c846 415 if (!m->unit)
fb6becb4 416 return 0;
9444b1f2 417
c00a4c8f 418 r = manager_stop_unit(m->manager, m->unit, &error, &job);
b92d0b4c
LP
419 if (r < 0) {
420 log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r));
421 sd_bus_error_free(&error);
422 } else
423 free_and_replace(m->scope_job, job);
424
425 q = manager_unref_unit(m->manager, m->unit, &error);
426 if (q < 0)
427 log_warning_errno(q, "Failed to drop reference to machine scope, ignoring: %s", bus_error_message(&error, r));
9444b1f2 428
b92d0b4c 429 return r;
9444b1f2
LP
430}
431
432int machine_stop(Machine *m) {
49f3fffd
LP
433 int r;
434 assert(m);
435
fbe55073
LP
436 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
437 return -EOPNOTSUPP;
438
49f3fffd
LP
439 r = machine_stop_scope(m);
440
441 m->stopping = true;
442
443 machine_save(m);
9fdcbae5 444 (void) manager_enqueue_nscd_cache_flush(m->manager);
49f3fffd
LP
445
446 return r;
447}
448
449int machine_finalize(Machine *m) {
9444b1f2
LP
450 assert(m);
451
452 if (m->started)
453 log_struct(LOG_INFO,
2b044526 454 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR,
9444b1f2 455 "NAME=%s", m->name,
de0671ee 456 "LEADER="PID_FMT, m->leader,
a1230ff9 457 LOG_MESSAGE("Machine %s terminated.", m->name));
9444b1f2 458
89f7c846 459 machine_unlink(m);
9444b1f2
LP
460 machine_add_to_gc_queue(m);
461
49f3fffd 462 if (m->started) {
9444b1f2 463 machine_send_signal(m, false);
49f3fffd
LP
464 m->started = false;
465 }
9444b1f2 466
49f3fffd 467 return 0;
9444b1f2
LP
468}
469
554ce41f 470bool machine_may_gc(Machine *m, bool drop_not_started) {
9444b1f2
LP
471 assert(m);
472
fbe55073 473 if (m->class == MACHINE_HOST)
554ce41f 474 return false;
fbe55073 475
9444b1f2 476 if (drop_not_started && !m->started)
554ce41f 477 return true;
9444b1f2 478
c3350683 479 if (m->scope_job && manager_job_is_active(m->manager, m->scope_job))
554ce41f 480 return false;
9444b1f2 481
89f7c846 482 if (m->unit && manager_unit_is_active(m->manager, m->unit))
554ce41f 483 return false;
9444b1f2 484
554ce41f 485 return true;
9444b1f2
LP
486}
487
488void machine_add_to_gc_queue(Machine *m) {
489 assert(m);
490
491 if (m->in_gc_queue)
492 return;
493
71fda00f 494 LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m);
9444b1f2
LP
495 m->in_gc_queue = true;
496}
497
fb6becb4
LP
498MachineState machine_get_state(Machine *s) {
499 assert(s);
9444b1f2 500
fbe55073
LP
501 if (s->class == MACHINE_HOST)
502 return MACHINE_RUNNING;
503
49f3fffd
LP
504 if (s->stopping)
505 return MACHINE_CLOSING;
506
fb6becb4 507 if (s->scope_job)
49f3fffd 508 return MACHINE_OPENING;
9444b1f2 509
fb6becb4
LP
510 return MACHINE_RUNNING;
511}
9444b1f2 512
fb6becb4
LP
513int machine_kill(Machine *m, KillWho who, int signo) {
514 assert(m);
9444b1f2 515
fbe55073
LP
516 if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER))
517 return -EOPNOTSUPP;
518
89f7c846 519 if (!m->unit)
fb6becb4 520 return -ESRCH;
9444b1f2 521
de58a50e
LP
522 if (who == KILL_LEADER) {
523 /* If we shall simply kill the leader, do so directly */
524
525 if (kill(m->leader, signo) < 0)
526 return -errno;
9d685ca8
ED
527
528 return 0;
de58a50e
LP
529 }
530
b938cb90 531 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
de58a50e 532 return manager_kill_unit(m->manager, m->unit, signo, NULL);
9444b1f2
LP
533}
534
fbe55073
LP
535int machine_openpt(Machine *m, int flags) {
536 assert(m);
537
538 switch (m->class) {
539
5f430ff7
LP
540 case MACHINE_HOST: {
541 int fd;
542
543 fd = posix_openpt(flags);
544 if (fd < 0)
545 return -errno;
546
547 if (unlockpt(fd) < 0)
548 return -errno;
549
550 return fd;
551 }
fbe55073
LP
552
553 case MACHINE_CONTAINER:
554 if (m->leader <= 0)
555 return -EINVAL;
556
557 return openpt_in_namespace(m->leader, flags);
558
559 default:
560 return -EOPNOTSUPP;
561 }
562}
563
40e1f4ea
LP
564int machine_open_terminal(Machine *m, const char *path, int mode) {
565 assert(m);
566
567 switch (m->class) {
568
569 case MACHINE_HOST:
570 return open_terminal(path, mode);
571
572 case MACHINE_CONTAINER:
573 if (m->leader <= 0)
574 return -EINVAL;
575
576 return open_terminal_in_namespace(m->leader, path, mode);
577
578 default:
579 return -EOPNOTSUPP;
580 }
581}
582
9b420b3c
LP
583void machine_release_unit(Machine *m) {
584 assert(m);
585
586 if (!m->unit)
587 return;
588
589 (void) hashmap_remove(m->manager->machine_units, m->unit);
a1e58e8e 590 m->unit = mfree(m->unit);
9b420b3c
LP
591}
592
3401419b 593int machine_get_uid_shift(Machine *m, uid_t *ret) {
fbd0b64f 594 char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
3401419b
LP
595 uid_t uid_base, uid_shift, uid_range;
596 gid_t gid_base, gid_shift, gid_range;
597 _cleanup_fclose_ FILE *f = NULL;
03a7dbea 598 int k, r;
3401419b
LP
599
600 assert(m);
601 assert(ret);
602
603 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
604 * mappings. In most cases setups should be simple like this, and administrators should only care about the
605 * basic offset a container has relative to the host. This is what this function exposes.
606 *
607 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
608
609 if (m->class == MACHINE_HOST) {
610 *ret = 0;
611 return 0;
612 }
613
614 if (m->class != MACHINE_CONTAINER)
615 return -EOPNOTSUPP;
616
617 xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader);
618 f = fopen(p, "re");
619 if (!f) {
620 if (errno == ENOENT) {
621 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
622 *ret = 0;
623 return 0;
624 }
625
626 return -errno;
627 }
628
629 /* Read the first line. There's at least one. */
630 errno = 0;
631 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range);
632 if (k != 3) {
633 if (ferror(f))
634 return -errno;
635
636 return -EBADMSG;
637 }
638
639 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
640 if (uid_base != 0)
641 return -ENXIO;
642 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
3a664727 643 if (uid_range < UID_NOBODY)
3401419b
LP
644 return -ENXIO;
645
646 /* If there's more than one line, then we don't support this mapping. */
03a7dbea
LP
647 r = safe_fgetc(f, NULL);
648 if (r < 0)
649 return r;
650 if (r != 0) /* Insist on EOF */
3401419b
LP
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. */
03a7dbea
LP
671 r = safe_fgetc(f, NULL);
672 if (r < 0)
673 return r;
674 if (r != 0) /* Insist on EOF */
3401419b
LP
675 return -ENXIO;
676
677 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
678 if (uid_base != (uid_t) gid_base)
679 return -ENXIO;
680 if (uid_shift != (uid_t) gid_shift)
681 return -ENXIO;
682 if (uid_range != (uid_t) gid_range)
683 return -ENXIO;
684
685 *ret = uid_shift;
686 return 0;
687}
688
9444b1f2
LP
689static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {
690 [MACHINE_CONTAINER] = "container",
fbe55073
LP
691 [MACHINE_VM] = "vm",
692 [MACHINE_HOST] = "host",
9444b1f2
LP
693};
694
695DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass);
fb6becb4
LP
696
697static const char* const machine_state_table[_MACHINE_STATE_MAX] = {
698 [MACHINE_OPENING] = "opening",
699 [MACHINE_RUNNING] = "running",
700 [MACHINE_CLOSING] = "closing"
701};
702
703DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState);
1ee306e1
LP
704
705static const char* const kill_who_table[_KILL_WHO_MAX] = {
706 [KILL_LEADER] = "leader",
707 [KILL_ALL] = "all"
708};
709
710DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);