1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "sd-messages.h"
9 #include "alloc-util.h"
10 #include "bus-error.h"
13 #include "errno-util.h"
15 #include "extract-word.h"
18 #include "format-util.h"
20 #include "machine-dbus.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "serialize.h"
28 #include "stdio-util.h"
29 #include "string-table.h"
30 #include "terminal-util.h"
31 #include "tmpfile-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
36 Machine
* machine_new(Manager
*manager
, MachineClass
class, const char *name
) {
40 assert(class < _MACHINE_CLASS_MAX
);
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. */
51 m
->name
= strdup(name
);
55 if (class != MACHINE_HOST
) {
56 m
->state_file
= path_join("/run/systemd/machines", m
->name
);
63 if (hashmap_put(manager
->machines
, m
->name
, m
) < 0)
76 Machine
* 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) fchmod(fileno(f
), 0644);
132 "# This is private data. Do not parse.\n"
137 _cleanup_free_
char *escaped
;
139 escaped
= cescape(m
->unit
);
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 */
149 fprintf(f
, "SCOPE_JOB=%s\n", m
->scope_job
);
152 _cleanup_free_
char *escaped
;
154 escaped
= cescape(m
->service
);
159 fprintf(f
, "SERVICE=%s\n", escaped
);
162 if (m
->root_directory
) {
163 _cleanup_free_
char *escaped
;
165 escaped
= cescape(m
->root_directory
);
170 fprintf(f
, "ROOT=%s\n", escaped
);
173 if (!sd_id128_is_null(m
->id
))
174 fprintf(f
, "ID=" SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->id
));
177 fprintf(f
, "LEADER="PID_FMT
"\n", m
->leader
);
179 if (m
->class != _MACHINE_CLASS_INVALID
)
180 fprintf(f
, "CLASS=%s\n", machine_class_to_string(m
->class));
182 if (dual_timestamp_is_set(&m
->timestamp
))
184 "REALTIME="USEC_FMT
"\n"
185 "MONOTONIC="USEC_FMT
"\n",
186 m
->timestamp
.realtime
,
187 m
->timestamp
.monotonic
);
189 if (m
->n_netif
> 0) {
194 for (i
= 0; i
< m
->n_netif
; i
++) {
198 fprintf(f
, "%i", m
->netif
[i
]);
204 r
= fflush_and_check(f
);
208 if (rename(temp_path
, m
->state_file
) < 0) {
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
);
226 (void) unlink(m
->state_file
);
229 (void) unlink(temp_path
);
231 return log_error_errno(r
, "Failed to save machine data %s: %m", m
->state_file
);
234 static void machine_unlink(Machine
*m
) {
240 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
245 (void) unlink(m
->state_file
);
248 int machine_load(Machine
*m
) {
249 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *id
= NULL
, *leader
= NULL
, *class = NULL
, *netif
= NULL
;
257 r
= parse_env_file(NULL
, m
->state_file
,
259 "SCOPE_JOB", &m
->scope_job
,
260 "SERVICE", &m
->service
,
261 "ROOT", &m
->root_directory
,
265 "REALTIME", &realtime
,
266 "MONOTONIC", &monotonic
,
272 return log_error_errno(r
, "Failed to read %s: %m", m
->state_file
);
276 sd_id128_from_string(id
, &m
->id
);
279 parse_pid(leader
, &m
->leader
);
284 c
= machine_class_from_string(class);
290 (void) deserialize_usec(realtime
, &m
->timestamp
.realtime
);
292 (void) deserialize_usec(monotonic
, &m
->timestamp
.monotonic
);
295 size_t allocated
= 0, nr
= 0;
301 _cleanup_free_
char *word
= NULL
;
304 r
= extract_first_word(&p
, &word
, NULL
, 0);
310 log_warning_errno(r
, "Failed to parse NETIF: %s", netif
);
314 if (parse_ifindex(word
, &ifi
) < 0)
317 if (!GREEDY_REALLOC(ni
, allocated
, nr
+1)) {
333 static int machine_start_scope(
338 const char *description
,
339 sd_bus_message
*more_properties
,
343 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
350 r
= sd_bus_message_new_method_call(
353 "org.freedesktop.systemd1",
354 "/org/freedesktop/systemd1",
355 "org.freedesktop.systemd1.Manager",
356 "StartTransientUnit");
360 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
364 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
368 if (!isempty(slice
)) {
369 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
374 if (!isempty(description
)) {
375 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
380 r
= sd_bus_message_append(m
, "(sv)(sv)(sv)(sv)(sv)",
381 "PIDs", "au", 1, pid
,
383 "CollectMode", "s", "inactive-or-failed",
385 "TasksMax", "t", UINT64_C(16384));
389 if (more_properties
) {
390 r
= sd_bus_message_copy(m
, more_properties
, true);
395 r
= sd_bus_message_close_container(m
);
399 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
403 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
411 r
= sd_bus_message_read(reply
, "o", &j
);
425 static int machine_ensure_scope(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
427 assert(m
->class != MACHINE_HOST
);
430 _cleanup_free_
char *escaped
= NULL
, *scope
= NULL
;
431 char *description
, *job
= NULL
;
434 escaped
= unit_name_escape(m
->name
);
438 scope
= strjoin("machine-", escaped
, ".scope");
442 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
444 r
= machine_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
446 return log_error_errno(r
, "Failed to start machine scope: %s", bus_error_message(error
, r
));
448 m
->unit
= TAKE_PTR(scope
);
449 m
->referenced
= true;
450 free_and_replace(m
->scope_job
, job
);
454 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
459 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
464 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
470 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
475 r
= machine_ensure_scope(m
, properties
, error
);
480 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR
,
482 "LEADER="PID_FMT
, m
->leader
,
483 LOG_MESSAGE("New machine %s.", m
->name
));
485 if (!dual_timestamp_is_set(&m
->timestamp
))
486 dual_timestamp_get(&m
->timestamp
);
490 /* Save new machine data */
493 machine_send_signal(m
, true);
494 (void) manager_enqueue_nscd_cache_flush(m
->manager
);
499 static int machine_stop_scope(Machine
*m
) {
500 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
505 assert(m
->class != MACHINE_HOST
);
510 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
512 log_error_errno(r
, "Failed to stop machine scope: %s", bus_error_message(&error
, r
));
513 sd_bus_error_free(&error
);
515 free_and_replace(m
->scope_job
, job
);
518 q
= manager_unref_unit(m
->manager
, m
->unit
, &error
);
520 log_warning_errno(q
, "Failed to drop reference to machine scope, ignoring: %s", bus_error_message(&error
, r
));
521 m
->referenced
= false;
527 int machine_stop(Machine
*m
) {
531 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
534 r
= machine_stop_scope(m
);
539 (void) manager_enqueue_nscd_cache_flush(m
->manager
);
544 int machine_finalize(Machine
*m
) {
549 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR
,
551 "LEADER="PID_FMT
, m
->leader
,
552 LOG_MESSAGE("Machine %s terminated.", m
->name
));
554 m
->stopping
= true; /* The machine is supposed to be going away. Don't try to kill it. */
558 machine_add_to_gc_queue(m
);
561 machine_send_signal(m
, false);
568 bool machine_may_gc(Machine
*m
, bool drop_not_started
) {
571 if (m
->class == MACHINE_HOST
)
574 if (drop_not_started
&& !m
->started
)
577 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
580 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
586 void machine_add_to_gc_queue(Machine
*m
) {
592 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
593 m
->in_gc_queue
= true;
596 MachineState
machine_get_state(Machine
*s
) {
599 if (s
->class == MACHINE_HOST
)
600 return MACHINE_RUNNING
;
603 return MACHINE_CLOSING
;
606 return MACHINE_OPENING
;
608 return MACHINE_RUNNING
;
611 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
614 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
620 if (who
== KILL_LEADER
) {
621 /* If we shall simply kill the leader, do so directly */
623 if (kill(m
->leader
, signo
) < 0)
629 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
630 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
633 int machine_openpt(Machine
*m
, int flags
, char **ret_slave
) {
640 return openpt_allocate(flags
, ret_slave
);
642 case MACHINE_CONTAINER
:
646 return openpt_allocate_in_namespace(m
->leader
, flags
, ret_slave
);
653 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
659 return open_terminal(path
, mode
);
661 case MACHINE_CONTAINER
:
665 return open_terminal_in_namespace(m
->leader
, path
, mode
);
672 void machine_release_unit(Machine
*m
) {
678 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
679 m
->unit
= mfree(m
->unit
);
682 int machine_get_uid_shift(Machine
*m
, uid_t
*ret
) {
683 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
684 uid_t uid_base
, uid_shift
, uid_range
;
685 gid_t gid_base
, gid_shift
, gid_range
;
686 _cleanup_fclose_
FILE *f
= NULL
;
692 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
693 * mappings. In most cases setups should be simple like this, and administrators should only care about the
694 * basic offset a container has relative to the host. This is what this function exposes.
696 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
698 if (m
->class == MACHINE_HOST
) {
703 if (m
->class != MACHINE_CONTAINER
)
706 xsprintf(p
, "/proc/" PID_FMT
"/uid_map", m
->leader
);
709 if (errno
== ENOENT
) {
710 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
718 /* Read the first line. There's at least one. */
720 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", &uid_base
, &uid_shift
, &uid_range
);
723 return errno_or_else(EIO
);
728 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
731 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
732 if (uid_range
< UID_NOBODY
)
735 /* If there's more than one line, then we don't support this mapping. */
736 r
= safe_fgetc(f
, NULL
);
739 if (r
!= 0) /* Insist on EOF */
744 xsprintf(p
, "/proc/" PID_FMT
"/gid_map", m
->leader
);
749 /* Read the first line. There's at least one. */
751 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
"\n", &gid_base
, &gid_shift
, &gid_range
);
754 return errno_or_else(EIO
);
759 /* If there's more than one line, then we don't support this file. */
760 r
= safe_fgetc(f
, NULL
);
763 if (r
!= 0) /* Insist on EOF */
766 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
767 if (uid_base
!= (uid_t
) gid_base
)
769 if (uid_shift
!= (uid_t
) gid_shift
)
771 if (uid_range
!= (uid_t
) gid_range
)
778 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
779 [MACHINE_CONTAINER
] = "container",
781 [MACHINE_HOST
] = "host",
784 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
786 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
787 [MACHINE_OPENING
] = "opening",
788 [MACHINE_RUNNING
] = "running",
789 [MACHINE_CLOSING
] = "closing"
792 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
794 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
795 [KILL_LEADER
] = "leader",
799 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);