1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
11 #include <stdio_ext.h>
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "bus-error.h"
19 #include "extract-word.h"
22 #include "format-util.h"
24 #include "machine-dbus.h"
27 #include "parse-util.h"
28 #include "process-util.h"
30 #include "stdio-util.h"
31 #include "string-table.h"
32 #include "terminal-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
37 Machine
* machine_new(Manager
*manager
, MachineClass
class, const char *name
) {
41 assert(class < _MACHINE_CLASS_MAX
);
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. */
52 m
->name
= strdup(name
);
56 if (class != MACHINE_HOST
) {
57 m
->state_file
= strappend("/run/systemd/machines/", m
->name
);
64 if (hashmap_put(manager
->machines
, m
->name
, m
) < 0)
77 void machine_free(Machine
*m
) {
81 operation_free(m
->operations
);
84 LIST_REMOVE(gc_queue
, m
->manager
->machine_gc_queue
, m
);
86 machine_release_unit(m
);
90 (void) hashmap_remove(m
->manager
->machines
, m
->name
);
92 if (m
->manager
->host_machine
== m
)
93 m
->manager
->host_machine
= NULL
;
96 (void) hashmap_remove_value(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
98 sd_bus_message_unref(m
->create_message
);
103 free(m
->root_directory
);
108 int machine_save(Machine
*m
) {
109 _cleanup_free_
char *temp_path
= NULL
;
110 _cleanup_fclose_
FILE *f
= NULL
;
121 r
= mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE
);
125 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
129 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
130 (void) fchmod(fileno(f
), 0644);
133 "# This is private data. Do not parse.\n"
138 _cleanup_free_
char *escaped
;
140 escaped
= cescape(m
->unit
);
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 */
150 fprintf(f
, "SCOPE_JOB=%s\n", m
->scope_job
);
153 _cleanup_free_
char *escaped
;
155 escaped
= cescape(m
->service
);
160 fprintf(f
, "SERVICE=%s\n", escaped
);
163 if (m
->root_directory
) {
164 _cleanup_free_
char *escaped
;
166 escaped
= cescape(m
->root_directory
);
171 fprintf(f
, "ROOT=%s\n", escaped
);
174 if (!sd_id128_is_null(m
->id
))
175 fprintf(f
, "ID=" SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->id
));
178 fprintf(f
, "LEADER="PID_FMT
"\n", m
->leader
);
180 if (m
->class != _MACHINE_CLASS_INVALID
)
181 fprintf(f
, "CLASS=%s\n", machine_class_to_string(m
->class));
183 if (dual_timestamp_is_set(&m
->timestamp
))
185 "REALTIME="USEC_FMT
"\n"
186 "MONOTONIC="USEC_FMT
"\n",
187 m
->timestamp
.realtime
,
188 m
->timestamp
.monotonic
);
190 if (m
->n_netif
> 0) {
195 for (i
= 0; i
< m
->n_netif
; i
++) {
199 fprintf(f
, "%i", m
->netif
[i
]);
205 r
= fflush_and_check(f
);
209 if (rename(temp_path
, m
->state_file
) < 0) {
217 /* Create a symlink from the unit name to the machine
218 * name, so that we can quickly find the machine for
219 * each given unit. Ignore error. */
220 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
221 (void) symlink(m
->name
, sl
);
227 (void) unlink(m
->state_file
);
230 (void) unlink(temp_path
);
232 return log_error_errno(r
, "Failed to save machine data %s: %m", m
->state_file
);
235 static void machine_unlink(Machine
*m
) {
241 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
246 (void) unlink(m
->state_file
);
249 int machine_load(Machine
*m
) {
250 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *id
= NULL
, *leader
= NULL
, *class = NULL
, *netif
= NULL
;
258 r
= parse_env_file(NULL
, m
->state_file
, NEWLINE
,
260 "SCOPE_JOB", &m
->scope_job
,
261 "SERVICE", &m
->service
,
262 "ROOT", &m
->root_directory
,
266 "REALTIME", &realtime
,
267 "MONOTONIC", &monotonic
,
274 return log_error_errno(r
, "Failed to read %s: %m", m
->state_file
);
278 sd_id128_from_string(id
, &m
->id
);
281 parse_pid(leader
, &m
->leader
);
286 c
= machine_class_from_string(class);
292 timestamp_deserialize(realtime
, &m
->timestamp
.realtime
);
294 timestamp_deserialize(monotonic
, &m
->timestamp
.monotonic
);
297 size_t allocated
= 0, nr
= 0;
303 _cleanup_free_
char *word
= NULL
;
306 r
= extract_first_word(&p
, &word
, NULL
, 0);
312 log_warning_errno(r
, "Failed to parse NETIF: %s", netif
);
316 if (parse_ifindex(word
, &ifi
) < 0)
319 if (!GREEDY_REALLOC(ni
, allocated
, nr
+1)) {
335 static int machine_start_scope(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
339 assert(m
->class != MACHINE_HOST
);
342 _cleanup_free_
char *escaped
= NULL
;
343 char *scope
, *description
, *job
= NULL
;
345 escaped
= unit_name_escape(m
->name
);
349 scope
= strjoin("machine-", escaped
, ".scope");
353 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
355 r
= manager_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
357 log_error("Failed to start machine scope: %s", bus_error_message(error
, r
));
369 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
374 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
379 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
385 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
390 r
= machine_start_scope(m
, properties
, error
);
395 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR
,
397 "LEADER="PID_FMT
, m
->leader
,
398 LOG_MESSAGE("New machine %s.", m
->name
));
400 if (!dual_timestamp_is_set(&m
->timestamp
))
401 dual_timestamp_get(&m
->timestamp
);
405 /* Save new machine data */
408 machine_send_signal(m
, true);
413 static int machine_stop_scope(Machine
*m
) {
414 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
419 assert(m
->class != MACHINE_HOST
);
424 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
426 log_error("Failed to stop machine scope: %s", bus_error_message(&error
, r
));
436 int machine_stop(Machine
*m
) {
440 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
443 r
= machine_stop_scope(m
);
452 int machine_finalize(Machine
*m
) {
457 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR
,
459 "LEADER="PID_FMT
, m
->leader
,
460 LOG_MESSAGE("Machine %s terminated.", m
->name
));
463 machine_add_to_gc_queue(m
);
466 machine_send_signal(m
, false);
473 bool machine_may_gc(Machine
*m
, bool drop_not_started
) {
476 if (m
->class == MACHINE_HOST
)
479 if (drop_not_started
&& !m
->started
)
482 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
485 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
491 void machine_add_to_gc_queue(Machine
*m
) {
497 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
498 m
->in_gc_queue
= true;
501 MachineState
machine_get_state(Machine
*s
) {
504 if (s
->class == MACHINE_HOST
)
505 return MACHINE_RUNNING
;
508 return MACHINE_CLOSING
;
511 return MACHINE_OPENING
;
513 return MACHINE_RUNNING
;
516 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
519 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
525 if (who
== KILL_LEADER
) {
526 /* If we shall simply kill the leader, do so directly */
528 if (kill(m
->leader
, signo
) < 0)
534 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
535 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
538 int machine_openpt(Machine
*m
, int flags
) {
546 fd
= posix_openpt(flags
);
550 if (unlockpt(fd
) < 0)
556 case MACHINE_CONTAINER
:
560 return openpt_in_namespace(m
->leader
, flags
);
567 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
573 return open_terminal(path
, mode
);
575 case MACHINE_CONTAINER
:
579 return open_terminal_in_namespace(m
->leader
, path
, mode
);
586 void machine_release_unit(Machine
*m
) {
592 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
593 m
->unit
= mfree(m
->unit
);
596 int machine_get_uid_shift(Machine
*m
, uid_t
*ret
) {
597 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
598 uid_t uid_base
, uid_shift
, uid_range
;
599 gid_t gid_base
, gid_shift
, gid_range
;
600 _cleanup_fclose_
FILE *f
= NULL
;
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.
610 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
612 if (m
->class == MACHINE_HOST
) {
617 if (m
->class != MACHINE_CONTAINER
)
620 xsprintf(p
, "/proc/" PID_FMT
"/uid_map", m
->leader
);
623 if (errno
== ENOENT
) {
624 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
632 /* Read the first line. There's at least one. */
634 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", &uid_base
, &uid_shift
, &uid_range
);
642 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
645 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
646 if (uid_range
< UID_NOBODY
)
649 /* If there's more than one line, then we don't support this mapping. */
655 xsprintf(p
, "/proc/" PID_FMT
"/gid_map", m
->leader
);
660 /* Read the first line. There's at least one. */
662 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
"\n", &gid_base
, &gid_shift
, &gid_range
);
670 /* If there's more than one line, then we don't support this file. */
674 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
675 if (uid_base
!= (uid_t
) gid_base
)
677 if (uid_shift
!= (uid_t
) gid_shift
)
679 if (uid_range
!= (uid_t
) gid_range
)
686 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
687 [MACHINE_CONTAINER
] = "container",
689 [MACHINE_HOST
] = "host",
692 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
694 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
695 [MACHINE_OPENING
] = "opening",
696 [MACHINE_RUNNING
] = "running",
697 [MACHINE_CLOSING
] = "closing"
700 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
702 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
703 [KILL_LEADER
] = "leader",
707 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);