]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machine.c
Merge pull request #24813 from DaanDeMeyer/at-all-the-things
[thirdparty/systemd.git] / src / machine / machine.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <unistd.h>
5 #include <sys/stat.h>
6
7 #include "sd-messages.h"
8
9 #include "alloc-util.h"
10 #include "bus-error.h"
11 #include "bus-locator.h"
12 #include "bus-util.h"
13 #include "env-file.h"
14 #include "errno-util.h"
15 #include "escape.h"
16 #include "extract-word.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "format-util.h"
20 #include "hashmap.h"
21 #include "machine-dbus.h"
22 #include "machine.h"
23 #include "mkdir-label.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "serialize.h"
28 #include "special.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
31 #include "terminal-util.h"
32 #include "tmpfile-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
35
36 Machine* machine_new(Manager *manager, MachineClass class, const char *name) {
37 Machine *m;
38
39 assert(manager);
40 assert(class < _MACHINE_CLASS_MAX);
41 assert(name);
42
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
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
55 if (class != MACHINE_HOST) {
56 m->state_file = path_join("/run/systemd/machines", m->name);
57 if (!m->state_file)
58 goto fail;
59 }
60
61 m->class = class;
62
63 if (hashmap_put(manager->machines, m->name, m) < 0)
64 goto fail;
65
66 m->manager = manager;
67
68 return m;
69
70 fail:
71 free(m->state_file);
72 free(m->name);
73 return mfree(m);
74 }
75
76 Machine* machine_free(Machine *m) {
77 if (!m)
78 return NULL;
79
80 while (m->operations)
81 operation_free(m->operations);
82
83 if (m->in_gc_queue)
84 LIST_REMOVE(gc_queue, m->manager->machine_gc_queue, m);
85
86 machine_release_unit(m);
87
88 free(m->scope_job);
89
90 (void) hashmap_remove(m->manager->machines, m->name);
91
92 if (m->manager->host_machine == m)
93 m->manager->host_machine = NULL;
94
95 if (m->leader > 0)
96 (void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
97
98 sd_bus_message_unref(m->create_message);
99
100 free(m->name);
101 free(m->state_file);
102 free(m->service);
103 free(m->root_directory);
104 free(m->netif);
105 return mfree(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);
114
115 if (!m->state_file)
116 return 0;
117
118 if (!m->started)
119 return 0;
120
121 r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE);
122 if (r < 0)
123 goto fail;
124
125 r = fopen_temporary(m->state_file, &f, &temp_path);
126 if (r < 0)
127 goto fail;
128
129 (void) fchmod(fileno(f), 0644);
130
131 fprintf(f,
132 "# This is private data. Do not parse.\n"
133 "NAME=%s\n",
134 m->name);
135
136 if (m->unit) {
137 _cleanup_free_ char *escaped = NULL;
138
139 escaped = cescape(m->unit);
140 if (!escaped) {
141 r = -ENOMEM;
142 goto fail;
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 }
147
148 if (m->scope_job)
149 fprintf(f, "SCOPE_JOB=%s\n", m->scope_job);
150
151 if (m->service) {
152 _cleanup_free_ char *escaped = NULL;
153
154 escaped = cescape(m->service);
155 if (!escaped) {
156 r = -ENOMEM;
157 goto fail;
158 }
159 fprintf(f, "SERVICE=%s\n", escaped);
160 }
161
162 if (m->root_directory) {
163 _cleanup_free_ char *escaped = NULL;
164
165 escaped = cescape(m->root_directory);
166 if (!escaped) {
167 r = -ENOMEM;
168 goto fail;
169 }
170 fprintf(f, "ROOT=%s\n", escaped);
171 }
172
173 if (!sd_id128_is_null(m->id))
174 fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id));
175
176 if (m->leader != 0)
177 fprintf(f, "LEADER="PID_FMT"\n", m->leader);
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,
184 "REALTIME="USEC_FMT"\n"
185 "MONOTONIC="USEC_FMT"\n",
186 m->timestamp.realtime,
187 m->timestamp.monotonic);
188
189 if (m->n_netif > 0) {
190 size_t i;
191
192 fputs("NETIF=", f);
193
194 for (i = 0; i < m->n_netif; i++) {
195 if (i != 0)
196 fputc(' ', f);
197
198 fprintf(f, "%i", m->netif[i]);
199 }
200
201 fputc('\n', f);
202 }
203
204 r = fflush_and_check(f);
205 if (r < 0)
206 goto fail;
207
208 if (rename(temp_path, m->state_file) < 0) {
209 r = -errno;
210 goto fail;
211 }
212
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
218 * each given unit. Ignore error. */
219 sl = strjoina("/run/systemd/machines/unit:", m->unit);
220 (void) symlink(m->name, sl);
221 }
222
223 return 0;
224
225 fail:
226 (void) unlink(m->state_file);
227
228 if (temp_path)
229 (void) unlink(temp_path);
230
231 return log_error_errno(r, "Failed to save machine data %s: %m", m->state_file);
232 }
233
234 static void machine_unlink(Machine *m) {
235 assert(m);
236
237 if (m->unit) {
238 char *sl;
239
240 sl = strjoina("/run/systemd/machines/unit:", m->unit);
241 (void) unlink(sl);
242 }
243
244 if (m->state_file)
245 (void) unlink(m->state_file);
246 }
247
248 int machine_load(Machine *m) {
249 _cleanup_free_ char *realtime = NULL, *monotonic = NULL, *id = NULL, *leader = NULL, *class = NULL, *netif = NULL;
250 int r;
251
252 assert(m);
253
254 if (!m->state_file)
255 return 0;
256
257 r = parse_env_file(NULL, m->state_file,
258 "SCOPE", &m->unit,
259 "SCOPE_JOB", &m->scope_job,
260 "SERVICE", &m->service,
261 "ROOT", &m->root_directory,
262 "ID", &id,
263 "LEADER", &leader,
264 "CLASS", &class,
265 "REALTIME", &realtime,
266 "MONOTONIC", &monotonic,
267 "NETIF", &netif);
268 if (r == -ENOENT)
269 return 0;
270 if (r < 0)
271 return log_error_errno(r, "Failed to read %s: %m", m->state_file);
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
287 if (realtime)
288 (void) deserialize_usec(realtime, &m->timestamp.realtime);
289 if (monotonic)
290 (void) deserialize_usec(monotonic, &m->timestamp.monotonic);
291
292 if (netif) {
293 _cleanup_free_ int *ni = NULL;
294 size_t nr = 0;
295 const char *p;
296
297 p = netif;
298 for (;;) {
299 _cleanup_free_ char *word = NULL;
300
301 r = extract_first_word(&p, &word, NULL, 0);
302 if (r == 0)
303 break;
304 if (r == -ENOMEM)
305 return log_oom();
306 if (r < 0) {
307 log_warning_errno(r, "Failed to parse NETIF: %s", netif);
308 break;
309 }
310
311 r = parse_ifindex(word);
312 if (r < 0)
313 continue;
314
315 if (!GREEDY_REALLOC(ni, nr + 1))
316 return log_oom();
317
318 ni[nr++] = r;
319 }
320
321 free_and_replace(m->netif, ni);
322 m->n_netif = nr;
323 }
324
325 return r;
326 }
327
328 static int machine_start_scope(
329 Machine *machine,
330 sd_bus_message *more_properties,
331 sd_bus_error *error) {
332
333 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
334 _cleanup_free_ char *escaped = NULL, *unit = NULL;
335 const char *description;
336 int r;
337
338 assert(machine);
339 assert(machine->leader > 0);
340 assert(!machine->unit);
341
342 escaped = unit_name_escape(machine->name);
343 if (!escaped)
344 return log_oom();
345
346 unit = strjoin("machine-", escaped, ".scope");
347 if (!unit)
348 return log_oom();
349
350 r = bus_message_new_method_call(
351 machine->manager->bus,
352 &m,
353 bus_systemd_mgr,
354 "StartTransientUnit");
355 if (r < 0)
356 return r;
357
358 r = sd_bus_message_append(m, "ss", unit, "fail");
359 if (r < 0)
360 return r;
361
362 r = sd_bus_message_open_container(m, 'a', "(sv)");
363 if (r < 0)
364 return r;
365
366 r = sd_bus_message_append(m, "(sv)", "Slice", "s", SPECIAL_MACHINE_SLICE);
367 if (r < 0)
368 return r;
369
370 description = strjoina(machine->class == MACHINE_VM ? "Virtual Machine " : "Container ", machine->name);
371 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
372 if (r < 0)
373 return r;
374
375 r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)",
376 "PIDs", "au", 1, machine->leader,
377 "Delegate", "b", 1,
378 "CollectMode", "s", "inactive-or-failed",
379 "AddRef", "b", 1,
380 "TasksMax", "t", UINT64_C(16384));
381 if (r < 0)
382 return r;
383
384 if (more_properties) {
385 r = sd_bus_message_copy(m, more_properties, true);
386 if (r < 0)
387 return r;
388 }
389
390 r = sd_bus_message_close_container(m);
391 if (r < 0)
392 return r;
393
394 r = sd_bus_message_append(m, "a(sa(sv))", 0);
395 if (r < 0)
396 return r;
397
398 r = sd_bus_call(NULL, m, 0, error, &reply);
399 if (r < 0)
400 return r;
401
402 machine->unit = TAKE_PTR(unit);
403 machine->referenced = true;
404
405 const char *job;
406 r = sd_bus_message_read(reply, "o", &job);
407 if (r < 0)
408 return r;
409
410 return free_and_strdup(&machine->scope_job, job);
411 }
412
413 static int machine_ensure_scope(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
414 int r;
415
416 assert(m);
417 assert(m->class != MACHINE_HOST);
418
419 if (!m->unit) {
420 r = machine_start_scope(m, properties, error);
421 if (r < 0)
422 return log_error_errno(r, "Failed to start machine scope: %s", bus_error_message(error, r));
423 }
424
425 assert(m->unit);
426 hashmap_put(m->manager->machine_units, m->unit, m);
427
428 return 0;
429 }
430
431 int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
432 int r;
433
434 assert(m);
435
436 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
437 return -EOPNOTSUPP;
438
439 if (m->started)
440 return 0;
441
442 r = hashmap_put(m->manager->machine_leaders, PID_TO_PTR(m->leader), m);
443 if (r < 0)
444 return r;
445
446 /* Create cgroup */
447 r = machine_ensure_scope(m, properties, error);
448 if (r < 0)
449 return r;
450
451 log_struct(LOG_INFO,
452 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR,
453 "NAME=%s", m->name,
454 "LEADER="PID_FMT, m->leader,
455 LOG_MESSAGE("New machine %s.", m->name));
456
457 if (!dual_timestamp_is_set(&m->timestamp))
458 dual_timestamp_get(&m->timestamp);
459
460 m->started = true;
461
462 /* Save new machine data */
463 machine_save(m);
464
465 machine_send_signal(m, true);
466 (void) manager_enqueue_nscd_cache_flush(m->manager);
467
468 return 0;
469 }
470
471 int machine_stop(Machine *m) {
472 int r;
473
474 assert(m);
475
476 if (!IN_SET(m->class, MACHINE_CONTAINER, MACHINE_VM))
477 return -EOPNOTSUPP;
478
479 if (m->unit) {
480 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
481 char *job = NULL;
482
483 r = manager_stop_unit(m->manager, m->unit, &error, &job);
484 if (r < 0)
485 return log_error_errno(r, "Failed to stop machine scope: %s", bus_error_message(&error, r));
486
487 free_and_replace(m->scope_job, job);
488 }
489
490 m->stopping = true;
491
492 machine_save(m);
493 (void) manager_enqueue_nscd_cache_flush(m->manager);
494
495 return 0;
496 }
497
498 int machine_finalize(Machine *m) {
499 assert(m);
500
501 if (m->started) {
502 log_struct(LOG_INFO,
503 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR,
504 "NAME=%s", m->name,
505 "LEADER="PID_FMT, m->leader,
506 LOG_MESSAGE("Machine %s terminated.", m->name));
507
508 m->stopping = true; /* The machine is supposed to be going away. Don't try to kill it. */
509 }
510
511 machine_unlink(m);
512 machine_add_to_gc_queue(m);
513
514 if (m->started) {
515 machine_send_signal(m, false);
516 m->started = false;
517 }
518
519 return 0;
520 }
521
522 bool machine_may_gc(Machine *m, bool drop_not_started) {
523 assert(m);
524
525 if (m->class == MACHINE_HOST)
526 return false;
527
528 if (drop_not_started && !m->started)
529 return true;
530
531 if (m->scope_job && manager_job_is_active(m->manager, m->scope_job))
532 return false;
533
534 if (m->unit && manager_unit_is_active(m->manager, m->unit))
535 return false;
536
537 return true;
538 }
539
540 void machine_add_to_gc_queue(Machine *m) {
541 assert(m);
542
543 if (m->in_gc_queue)
544 return;
545
546 LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m);
547 m->in_gc_queue = true;
548 }
549
550 MachineState machine_get_state(Machine *s) {
551 assert(s);
552
553 if (s->class == MACHINE_HOST)
554 return MACHINE_RUNNING;
555
556 if (s->stopping)
557 return MACHINE_CLOSING;
558
559 if (s->scope_job)
560 return MACHINE_OPENING;
561
562 return MACHINE_RUNNING;
563 }
564
565 int machine_kill(Machine *m, KillWho who, int signo) {
566 assert(m);
567
568 if (!IN_SET(m->class, MACHINE_VM, MACHINE_CONTAINER))
569 return -EOPNOTSUPP;
570
571 if (!m->unit)
572 return -ESRCH;
573
574 if (who == KILL_LEADER) /* If we shall simply kill the leader, do so directly */
575 return RET_NERRNO(kill(m->leader, signo));
576
577 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
578 return manager_kill_unit(m->manager, m->unit, signo, NULL);
579 }
580
581 int machine_openpt(Machine *m, int flags, char **ret_slave) {
582 assert(m);
583
584 switch (m->class) {
585
586 case MACHINE_HOST:
587
588 return openpt_allocate(flags, ret_slave);
589
590 case MACHINE_CONTAINER:
591 if (m->leader <= 0)
592 return -EINVAL;
593
594 return openpt_allocate_in_namespace(m->leader, flags, ret_slave);
595
596 default:
597 return -EOPNOTSUPP;
598 }
599 }
600
601 int machine_open_terminal(Machine *m, const char *path, int mode) {
602 assert(m);
603
604 switch (m->class) {
605
606 case MACHINE_HOST:
607 return open_terminal(path, mode);
608
609 case MACHINE_CONTAINER:
610 if (m->leader <= 0)
611 return -EINVAL;
612
613 return open_terminal_in_namespace(m->leader, path, mode);
614
615 default:
616 return -EOPNOTSUPP;
617 }
618 }
619
620 void machine_release_unit(Machine *m) {
621 assert(m);
622
623 if (!m->unit)
624 return;
625
626 if (m->referenced) {
627 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
628 int r;
629
630 r = manager_unref_unit(m->manager, m->unit, &error);
631 if (r < 0)
632 log_warning_errno(r, "Failed to drop reference to machine scope, ignoring: %s",
633 bus_error_message(&error, r));
634
635 m->referenced = false;
636 }
637
638 (void) hashmap_remove(m->manager->machine_units, m->unit);
639 m->unit = mfree(m->unit);
640 }
641
642 int machine_get_uid_shift(Machine *m, uid_t *ret) {
643 char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
644 uid_t uid_base, uid_shift, uid_range;
645 gid_t gid_base, gid_shift, gid_range;
646 _cleanup_fclose_ FILE *f = NULL;
647 int k, r;
648
649 assert(m);
650 assert(ret);
651
652 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
653 * mappings. In most cases setups should be simple like this, and administrators should only care about the
654 * basic offset a container has relative to the host. This is what this function exposes.
655 *
656 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
657
658 if (m->class == MACHINE_HOST) {
659 *ret = 0;
660 return 0;
661 }
662
663 if (m->class != MACHINE_CONTAINER)
664 return -EOPNOTSUPP;
665
666 xsprintf(p, "/proc/" PID_FMT "/uid_map", m->leader);
667 f = fopen(p, "re");
668 if (!f) {
669 if (errno == ENOENT) {
670 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
671 *ret = 0;
672 return 0;
673 }
674
675 return -errno;
676 }
677
678 /* Read the first line. There's at least one. */
679 errno = 0;
680 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT "\n", &uid_base, &uid_shift, &uid_range);
681 if (k != 3) {
682 if (ferror(f))
683 return errno_or_else(EIO);
684
685 return -EBADMSG;
686 }
687
688 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
689 if (uid_base != 0)
690 return -ENXIO;
691 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
692 if (uid_range < UID_NOBODY)
693 return -ENXIO;
694
695 /* If there's more than one line, then we don't support this mapping. */
696 r = safe_fgetc(f, NULL);
697 if (r < 0)
698 return r;
699 if (r != 0) /* Insist on EOF */
700 return -ENXIO;
701
702 fclose(f);
703
704 xsprintf(p, "/proc/" PID_FMT "/gid_map", m->leader);
705 f = fopen(p, "re");
706 if (!f)
707 return -errno;
708
709 /* Read the first line. There's at least one. */
710 errno = 0;
711 k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT "\n", &gid_base, &gid_shift, &gid_range);
712 if (k != 3) {
713 if (ferror(f))
714 return errno_or_else(EIO);
715
716 return -EBADMSG;
717 }
718
719 /* If there's more than one line, then we don't support this file. */
720 r = safe_fgetc(f, NULL);
721 if (r < 0)
722 return r;
723 if (r != 0) /* Insist on EOF */
724 return -ENXIO;
725
726 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
727 if (uid_base != (uid_t) gid_base)
728 return -ENXIO;
729 if (uid_shift != (uid_t) gid_shift)
730 return -ENXIO;
731 if (uid_range != (uid_t) gid_range)
732 return -ENXIO;
733
734 *ret = uid_shift;
735 return 0;
736 }
737
738 static int machine_owns_uid_internal(
739 Machine *machine,
740 const char *map_file, /* "uid_map" or "gid_map" */
741 uid_t uid,
742 uid_t *ret_internal_uid) {
743
744 _cleanup_fclose_ FILE *f = NULL;
745 const char *p;
746
747 /* This is a generic implementation for both uids and gids, under the assumptions they have the same types and semantics. */
748 assert_cc(sizeof(uid_t) == sizeof(gid_t));
749
750 assert(machine);
751
752 /* Checks if the specified host UID is owned by the machine, and returns the UID it maps to
753 * internally in the machine */
754
755 if (machine->class != MACHINE_CONTAINER)
756 goto negative;
757
758 p = procfs_file_alloca(machine->leader, map_file);
759 f = fopen(p, "re");
760 if (!f) {
761 log_debug_errno(errno, "Failed to open %s, ignoring.", p);
762 goto negative;
763 }
764
765 for (;;) {
766 uid_t uid_base, uid_shift, uid_range, converted;
767 int k;
768
769 errno = 0;
770 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
771 if (k < 0 && feof(f))
772 break;
773 if (k != 3) {
774 if (ferror(f))
775 return errno_or_else(EIO);
776
777 return -EIO;
778 }
779
780 /* The private user namespace is disabled, ignoring. */
781 if (uid_shift == 0)
782 continue;
783
784 if (uid < uid_shift || uid >= uid_shift + uid_range)
785 continue;
786
787 converted = (uid - uid_shift + uid_base);
788 if (!uid_is_valid(converted))
789 return -EINVAL;
790
791 if (ret_internal_uid)
792 *ret_internal_uid = converted;
793
794 return true;
795 }
796
797 negative:
798 if (ret_internal_uid)
799 *ret_internal_uid = UID_INVALID;
800
801 return false;
802 }
803
804 int machine_owns_uid(Machine *machine, uid_t uid, uid_t *ret_internal_uid) {
805 return machine_owns_uid_internal(machine, "uid_map", uid, ret_internal_uid);
806 }
807
808 int machine_owns_gid(Machine *machine, gid_t gid, gid_t *ret_internal_gid) {
809 return machine_owns_uid_internal(machine, "gid_map", (uid_t) gid, (uid_t*) ret_internal_gid);
810 }
811
812 static int machine_translate_uid_internal(
813 Machine *machine,
814 const char *map_file, /* "uid_map" or "gid_map" */
815 uid_t uid,
816 uid_t *ret_host_uid) {
817
818 _cleanup_fclose_ FILE *f = NULL;
819 const char *p;
820
821 /* This is a generic implementation for both uids and gids, under the assumptions they have the same types and semantics. */
822 assert_cc(sizeof(uid_t) == sizeof(gid_t));
823
824 assert(machine);
825 assert(uid_is_valid(uid));
826
827 if (machine->class != MACHINE_CONTAINER)
828 return -ESRCH;
829
830 /* Translates a machine UID into a host UID */
831
832 p = procfs_file_alloca(machine->leader, map_file);
833 f = fopen(p, "re");
834 if (!f)
835 return -errno;
836
837 for (;;) {
838 uid_t uid_base, uid_shift, uid_range, converted;
839 int k;
840
841 errno = 0;
842 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
843 if (k < 0 && feof(f))
844 break;
845 if (k != 3) {
846 if (ferror(f))
847 return errno_or_else(EIO);
848
849 return -EIO;
850 }
851
852 if (uid < uid_base || uid >= uid_base + uid_range)
853 continue;
854
855 converted = uid - uid_base + uid_shift;
856 if (!uid_is_valid(converted))
857 return -EINVAL;
858
859 if (ret_host_uid)
860 *ret_host_uid = converted;
861 return 0;
862 }
863
864 return -ESRCH;
865 }
866
867 int machine_translate_uid(Machine *machine, gid_t uid, gid_t *ret_host_uid) {
868 return machine_translate_uid_internal(machine, "uid_map", uid, ret_host_uid);
869 }
870
871 int machine_translate_gid(Machine *machine, gid_t gid, gid_t *ret_host_gid) {
872 return machine_translate_uid_internal(machine, "gid_map", (uid_t) gid, (uid_t*) ret_host_gid);
873 }
874
875 static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {
876 [MACHINE_CONTAINER] = "container",
877 [MACHINE_VM] = "vm",
878 [MACHINE_HOST] = "host",
879 };
880
881 DEFINE_STRING_TABLE_LOOKUP(machine_class, MachineClass);
882
883 static const char* const machine_state_table[_MACHINE_STATE_MAX] = {
884 [MACHINE_OPENING] = "opening",
885 [MACHINE_RUNNING] = "running",
886 [MACHINE_CLOSING] = "closing"
887 };
888
889 DEFINE_STRING_TABLE_LOOKUP(machine_state, MachineState);
890
891 static const char* const kill_who_table[_KILL_WHO_MAX] = {
892 [KILL_LEADER] = "leader",
893 [KILL_ALL] = "all"
894 };
895
896 DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);