1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "bus-error.h"
14 #include "extract-word.h"
17 #include "format-util.h"
19 #include "machine-dbus.h"
22 #include "parse-util.h"
23 #include "process-util.h"
25 #include "stdio-util.h"
26 #include "string-table.h"
27 #include "terminal-util.h"
28 #include "unit-name.h"
29 #include "user-util.h"
32 Machine
* machine_new(Manager
*manager
, MachineClass
class, const char *name
) {
36 assert(class < _MACHINE_CLASS_MAX
);
39 /* Passing class == _MACHINE_CLASS_INVALID here is fine. It
40 * means as much as "we don't know yet", and that we'll figure
41 * it out later when loading the state file. */
47 m
->name
= strdup(name
);
51 if (class != MACHINE_HOST
) {
52 m
->state_file
= strappend("/run/systemd/machines/", m
->name
);
59 if (hashmap_put(manager
->machines
, m
->name
, m
) < 0)
72 void machine_free(Machine
*m
) {
76 operation_free(m
->operations
);
79 LIST_REMOVE(gc_queue
, m
->manager
->machine_gc_queue
, m
);
81 machine_release_unit(m
);
85 (void) hashmap_remove(m
->manager
->machines
, m
->name
);
87 if (m
->manager
->host_machine
== m
)
88 m
->manager
->host_machine
= NULL
;
91 (void) hashmap_remove_value(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
93 sd_bus_message_unref(m
->create_message
);
98 free(m
->root_directory
);
103 int machine_save(Machine
*m
) {
104 _cleanup_free_
char *temp_path
= NULL
;
105 _cleanup_fclose_
FILE *f
= NULL
;
116 r
= mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE
);
120 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
124 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
125 (void) fchmod(fileno(f
), 0644);
128 "# This is private data. Do not parse.\n"
133 _cleanup_free_
char *escaped
;
135 escaped
= cescape(m
->unit
);
141 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 */
145 fprintf(f
, "SCOPE_JOB=%s\n", m
->scope_job
);
148 _cleanup_free_
char *escaped
;
150 escaped
= cescape(m
->service
);
155 fprintf(f
, "SERVICE=%s\n", escaped
);
158 if (m
->root_directory
) {
159 _cleanup_free_
char *escaped
;
161 escaped
= cescape(m
->root_directory
);
166 fprintf(f
, "ROOT=%s\n", escaped
);
169 if (!sd_id128_is_null(m
->id
))
170 fprintf(f
, "ID=" SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->id
));
173 fprintf(f
, "LEADER="PID_FMT
"\n", m
->leader
);
175 if (m
->class != _MACHINE_CLASS_INVALID
)
176 fprintf(f
, "CLASS=%s\n", machine_class_to_string(m
->class));
178 if (dual_timestamp_is_set(&m
->timestamp
))
180 "REALTIME="USEC_FMT
"\n"
181 "MONOTONIC="USEC_FMT
"\n",
182 m
->timestamp
.realtime
,
183 m
->timestamp
.monotonic
);
185 if (m
->n_netif
> 0) {
190 for (i
= 0; i
< m
->n_netif
; i
++) {
194 fprintf(f
, "%i", m
->netif
[i
]);
200 r
= fflush_and_check(f
);
204 if (rename(temp_path
, m
->state_file
) < 0) {
212 /* Create a symlink from the unit name to the machine
213 * name, so that we can quickly find the machine for
214 * each given unit. Ignore error. */
215 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
216 (void) symlink(m
->name
, sl
);
222 (void) unlink(m
->state_file
);
225 (void) unlink(temp_path
);
227 return log_error_errno(r
, "Failed to save machine data %s: %m", m
->state_file
);
230 static void machine_unlink(Machine
*m
) {
236 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
241 (void) unlink(m
->state_file
);
244 int machine_load(Machine
*m
) {
245 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *id
= NULL
, *leader
= NULL
, *class = NULL
, *netif
= NULL
;
253 r
= parse_env_file(NULL
, m
->state_file
, NEWLINE
,
255 "SCOPE_JOB", &m
->scope_job
,
256 "SERVICE", &m
->service
,
257 "ROOT", &m
->root_directory
,
261 "REALTIME", &realtime
,
262 "MONOTONIC", &monotonic
,
269 return log_error_errno(r
, "Failed to read %s: %m", m
->state_file
);
273 sd_id128_from_string(id
, &m
->id
);
276 parse_pid(leader
, &m
->leader
);
281 c
= machine_class_from_string(class);
287 timestamp_deserialize(realtime
, &m
->timestamp
.realtime
);
289 timestamp_deserialize(monotonic
, &m
->timestamp
.monotonic
);
292 size_t allocated
= 0, nr
= 0;
298 _cleanup_free_
char *word
= NULL
;
301 r
= extract_first_word(&p
, &word
, NULL
, 0);
307 log_warning_errno(r
, "Failed to parse NETIF: %s", netif
);
311 if (parse_ifindex(word
, &ifi
) < 0)
314 if (!GREEDY_REALLOC(ni
, allocated
, nr
+1)) {
330 static int machine_start_scope(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
334 assert(m
->class != MACHINE_HOST
);
337 _cleanup_free_
char *escaped
= NULL
;
338 char *scope
, *description
, *job
= NULL
;
340 escaped
= unit_name_escape(m
->name
);
344 scope
= strjoin("machine-", escaped
, ".scope");
348 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
350 r
= manager_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
352 log_error("Failed to start machine scope: %s", bus_error_message(error
, r
));
364 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
369 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
374 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
380 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
385 r
= machine_start_scope(m
, properties
, error
);
390 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR
,
392 "LEADER="PID_FMT
, m
->leader
,
393 LOG_MESSAGE("New machine %s.", m
->name
));
395 if (!dual_timestamp_is_set(&m
->timestamp
))
396 dual_timestamp_get(&m
->timestamp
);
400 /* Save new machine data */
403 machine_send_signal(m
, true);
408 static int machine_stop_scope(Machine
*m
) {
409 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
414 assert(m
->class != MACHINE_HOST
);
419 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
421 log_error("Failed to stop machine scope: %s", bus_error_message(&error
, r
));
431 int machine_stop(Machine
*m
) {
435 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
438 r
= machine_stop_scope(m
);
447 int machine_finalize(Machine
*m
) {
452 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR
,
454 "LEADER="PID_FMT
, m
->leader
,
455 LOG_MESSAGE("Machine %s terminated.", m
->name
));
458 machine_add_to_gc_queue(m
);
461 machine_send_signal(m
, false);
468 bool machine_may_gc(Machine
*m
, bool drop_not_started
) {
471 if (m
->class == MACHINE_HOST
)
474 if (drop_not_started
&& !m
->started
)
477 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
480 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
486 void machine_add_to_gc_queue(Machine
*m
) {
492 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
493 m
->in_gc_queue
= true;
496 MachineState
machine_get_state(Machine
*s
) {
499 if (s
->class == MACHINE_HOST
)
500 return MACHINE_RUNNING
;
503 return MACHINE_CLOSING
;
506 return MACHINE_OPENING
;
508 return MACHINE_RUNNING
;
511 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
514 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
520 if (who
== KILL_LEADER
) {
521 /* If we shall simply kill the leader, do so directly */
523 if (kill(m
->leader
, signo
) < 0)
529 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
530 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
533 int machine_openpt(Machine
*m
, int flags
) {
541 fd
= posix_openpt(flags
);
545 if (unlockpt(fd
) < 0)
551 case MACHINE_CONTAINER
:
555 return openpt_in_namespace(m
->leader
, flags
);
562 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
568 return open_terminal(path
, mode
);
570 case MACHINE_CONTAINER
:
574 return open_terminal_in_namespace(m
->leader
, path
, mode
);
581 void machine_release_unit(Machine
*m
) {
587 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
588 m
->unit
= mfree(m
->unit
);
591 int machine_get_uid_shift(Machine
*m
, uid_t
*ret
) {
592 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
593 uid_t uid_base
, uid_shift
, uid_range
;
594 gid_t gid_base
, gid_shift
, gid_range
;
595 _cleanup_fclose_
FILE *f
= NULL
;
601 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
602 * mappings. In most cases setups should be simple like this, and administrators should only care about the
603 * basic offset a container has relative to the host. This is what this function exposes.
605 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
607 if (m
->class == MACHINE_HOST
) {
612 if (m
->class != MACHINE_CONTAINER
)
615 xsprintf(p
, "/proc/" PID_FMT
"/uid_map", m
->leader
);
618 if (errno
== ENOENT
) {
619 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
627 /* Read the first line. There's at least one. */
629 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", &uid_base
, &uid_shift
, &uid_range
);
637 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
640 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
641 if (uid_range
< UID_NOBODY
)
644 /* If there's more than one line, then we don't support this mapping. */
650 xsprintf(p
, "/proc/" PID_FMT
"/gid_map", m
->leader
);
655 /* Read the first line. There's at least one. */
657 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
"\n", &gid_base
, &gid_shift
, &gid_range
);
665 /* If there's more than one line, then we don't support this file. */
669 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
670 if (uid_base
!= (uid_t
) gid_base
)
672 if (uid_shift
!= (uid_t
) gid_shift
)
674 if (uid_range
!= (uid_t
) gid_range
)
681 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
682 [MACHINE_CONTAINER
] = "container",
684 [MACHINE_HOST
] = "host",
687 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
689 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
690 [MACHINE_OPENING
] = "opening",
691 [MACHINE_RUNNING
] = "running",
692 [MACHINE_CLOSING
] = "closing"
695 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
697 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
698 [KILL_LEADER
] = "leader",
702 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);