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"
24 #include "serialize.h"
26 #include "stdio-util.h"
27 #include "string-table.h"
28 #include "terminal-util.h"
29 #include "unit-name.h"
30 #include "user-util.h"
33 Machine
* machine_new(Manager
*manager
, MachineClass
class, const char *name
) {
37 assert(class < _MACHINE_CLASS_MAX
);
40 /* Passing class == _MACHINE_CLASS_INVALID here is fine. It
41 * means as much as "we don't know yet", and that we'll figure
42 * it out later when loading the state file. */
48 m
->name
= strdup(name
);
52 if (class != MACHINE_HOST
) {
53 m
->state_file
= strappend("/run/systemd/machines/", m
->name
);
60 if (hashmap_put(manager
->machines
, m
->name
, m
) < 0)
73 void machine_free(Machine
*m
) {
77 operation_free(m
->operations
);
80 LIST_REMOVE(gc_queue
, m
->manager
->machine_gc_queue
, m
);
82 machine_release_unit(m
);
86 (void) hashmap_remove(m
->manager
->machines
, m
->name
);
88 if (m
->manager
->host_machine
== m
)
89 m
->manager
->host_machine
= NULL
;
92 (void) hashmap_remove_value(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
94 sd_bus_message_unref(m
->create_message
);
99 free(m
->root_directory
);
104 int machine_save(Machine
*m
) {
105 _cleanup_free_
char *temp_path
= NULL
;
106 _cleanup_fclose_
FILE *f
= NULL
;
117 r
= mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE
);
121 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
125 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
126 (void) fchmod(fileno(f
), 0644);
129 "# This is private data. Do not parse.\n"
134 _cleanup_free_
char *escaped
;
136 escaped
= cescape(m
->unit
);
142 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 fprintf(f
, "SCOPE_JOB=%s\n", m
->scope_job
);
149 _cleanup_free_
char *escaped
;
151 escaped
= cescape(m
->service
);
156 fprintf(f
, "SERVICE=%s\n", escaped
);
159 if (m
->root_directory
) {
160 _cleanup_free_
char *escaped
;
162 escaped
= cescape(m
->root_directory
);
167 fprintf(f
, "ROOT=%s\n", escaped
);
170 if (!sd_id128_is_null(m
->id
))
171 fprintf(f
, "ID=" SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->id
));
174 fprintf(f
, "LEADER="PID_FMT
"\n", m
->leader
);
176 if (m
->class != _MACHINE_CLASS_INVALID
)
177 fprintf(f
, "CLASS=%s\n", machine_class_to_string(m
->class));
179 if (dual_timestamp_is_set(&m
->timestamp
))
181 "REALTIME="USEC_FMT
"\n"
182 "MONOTONIC="USEC_FMT
"\n",
183 m
->timestamp
.realtime
,
184 m
->timestamp
.monotonic
);
186 if (m
->n_netif
> 0) {
191 for (i
= 0; i
< m
->n_netif
; i
++) {
195 fprintf(f
, "%i", m
->netif
[i
]);
201 r
= fflush_and_check(f
);
205 if (rename(temp_path
, m
->state_file
) < 0) {
213 /* Create a symlink from the unit name to the machine
214 * name, so that we can quickly find the machine for
215 * each given unit. Ignore error. */
216 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
217 (void) symlink(m
->name
, sl
);
223 (void) unlink(m
->state_file
);
226 (void) unlink(temp_path
);
228 return log_error_errno(r
, "Failed to save machine data %s: %m", m
->state_file
);
231 static void machine_unlink(Machine
*m
) {
237 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
242 (void) unlink(m
->state_file
);
245 int machine_load(Machine
*m
) {
246 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *id
= NULL
, *leader
= NULL
, *class = NULL
, *netif
= NULL
;
254 r
= parse_env_file(NULL
, m
->state_file
,
256 "SCOPE_JOB", &m
->scope_job
,
257 "SERVICE", &m
->service
,
258 "ROOT", &m
->root_directory
,
262 "REALTIME", &realtime
,
263 "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 (void) deserialize_usec(realtime
, &m
->timestamp
.realtime
);
289 (void) deserialize_usec(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
) {
332 assert(m
->class != MACHINE_HOST
);
335 _cleanup_free_
char *escaped
= NULL
, *scope
= NULL
;
336 char *description
, *job
= NULL
;
339 escaped
= unit_name_escape(m
->name
);
343 scope
= strjoin("machine-", escaped
, ".scope");
347 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
349 r
= manager_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
351 return log_error_errno(r
, "Failed to start machine scope: %s", bus_error_message(error
, r
));
353 m
->unit
= TAKE_PTR(scope
);
354 free_and_replace(m
->scope_job
, job
);
358 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
363 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
368 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
374 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
379 r
= machine_start_scope(m
, properties
, error
);
384 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR
,
386 "LEADER="PID_FMT
, m
->leader
,
387 LOG_MESSAGE("New machine %s.", m
->name
));
389 if (!dual_timestamp_is_set(&m
->timestamp
))
390 dual_timestamp_get(&m
->timestamp
);
394 /* Save new machine data */
397 machine_send_signal(m
, true);
402 static int machine_stop_scope(Machine
*m
) {
403 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
408 assert(m
->class != MACHINE_HOST
);
413 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
415 log_error_errno(r
, "Failed to stop machine scope: %s", bus_error_message(&error
, r
));
416 sd_bus_error_free(&error
);
418 free_and_replace(m
->scope_job
, job
);
420 q
= manager_unref_unit(m
->manager
, m
->unit
, &error
);
422 log_warning_errno(q
, "Failed to drop reference to machine scope, ignoring: %s", bus_error_message(&error
, r
));
427 int machine_stop(Machine
*m
) {
431 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
434 r
= machine_stop_scope(m
);
443 int machine_finalize(Machine
*m
) {
448 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR
,
450 "LEADER="PID_FMT
, m
->leader
,
451 LOG_MESSAGE("Machine %s terminated.", m
->name
));
454 machine_add_to_gc_queue(m
);
457 machine_send_signal(m
, false);
464 bool machine_may_gc(Machine
*m
, bool drop_not_started
) {
467 if (m
->class == MACHINE_HOST
)
470 if (drop_not_started
&& !m
->started
)
473 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
476 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
482 void machine_add_to_gc_queue(Machine
*m
) {
488 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
489 m
->in_gc_queue
= true;
492 MachineState
machine_get_state(Machine
*s
) {
495 if (s
->class == MACHINE_HOST
)
496 return MACHINE_RUNNING
;
499 return MACHINE_CLOSING
;
502 return MACHINE_OPENING
;
504 return MACHINE_RUNNING
;
507 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
510 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
516 if (who
== KILL_LEADER
) {
517 /* If we shall simply kill the leader, do so directly */
519 if (kill(m
->leader
, signo
) < 0)
525 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
526 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
529 int machine_openpt(Machine
*m
, int flags
) {
537 fd
= posix_openpt(flags
);
541 if (unlockpt(fd
) < 0)
547 case MACHINE_CONTAINER
:
551 return openpt_in_namespace(m
->leader
, flags
);
558 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
564 return open_terminal(path
, mode
);
566 case MACHINE_CONTAINER
:
570 return open_terminal_in_namespace(m
->leader
, path
, mode
);
577 void machine_release_unit(Machine
*m
) {
583 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
584 m
->unit
= mfree(m
->unit
);
587 int machine_get_uid_shift(Machine
*m
, uid_t
*ret
) {
588 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
589 uid_t uid_base
, uid_shift
, uid_range
;
590 gid_t gid_base
, gid_shift
, gid_range
;
591 _cleanup_fclose_
FILE *f
= NULL
;
597 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
598 * mappings. In most cases setups should be simple like this, and administrators should only care about the
599 * basic offset a container has relative to the host. This is what this function exposes.
601 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
603 if (m
->class == MACHINE_HOST
) {
608 if (m
->class != MACHINE_CONTAINER
)
611 xsprintf(p
, "/proc/" PID_FMT
"/uid_map", m
->leader
);
614 if (errno
== ENOENT
) {
615 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
623 /* Read the first line. There's at least one. */
625 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", &uid_base
, &uid_shift
, &uid_range
);
633 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
636 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
637 if (uid_range
< UID_NOBODY
)
640 /* If there's more than one line, then we don't support this mapping. */
646 xsprintf(p
, "/proc/" PID_FMT
"/gid_map", m
->leader
);
651 /* Read the first line. There's at least one. */
653 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
"\n", &gid_base
, &gid_shift
, &gid_range
);
661 /* If there's more than one line, then we don't support this file. */
665 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
666 if (uid_base
!= (uid_t
) gid_base
)
668 if (uid_shift
!= (uid_t
) gid_shift
)
670 if (uid_range
!= (uid_t
) gid_range
)
677 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
678 [MACHINE_CONTAINER
] = "container",
680 [MACHINE_HOST
] = "host",
683 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
685 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
686 [MACHINE_OPENING
] = "opening",
687 [MACHINE_RUNNING
] = "running",
688 [MACHINE_CLOSING
] = "closing"
691 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
693 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
694 [KILL_LEADER
] = "leader",
698 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);