1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "bus-error.h"
14 #include "errno-util.h"
16 #include "extract-word.h"
19 #include "format-util.h"
21 #include "machine-dbus.h"
24 #include "parse-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
= strappend("/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(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
335 assert(m
->class != MACHINE_HOST
);
338 _cleanup_free_
char *escaped
= NULL
, *scope
= NULL
;
339 char *description
, *job
= NULL
;
342 escaped
= unit_name_escape(m
->name
);
346 scope
= strjoin("machine-", escaped
, ".scope");
350 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
352 r
= manager_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
354 return log_error_errno(r
, "Failed to start machine scope: %s", bus_error_message(error
, r
));
356 m
->unit
= TAKE_PTR(scope
);
357 free_and_replace(m
->scope_job
, job
);
361 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
366 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
371 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
377 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
382 r
= machine_start_scope(m
, properties
, error
);
387 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR
,
389 "LEADER="PID_FMT
, m
->leader
,
390 LOG_MESSAGE("New machine %s.", m
->name
));
392 if (!dual_timestamp_is_set(&m
->timestamp
))
393 dual_timestamp_get(&m
->timestamp
);
397 /* Save new machine data */
400 machine_send_signal(m
, true);
401 (void) manager_enqueue_nscd_cache_flush(m
->manager
);
406 static int machine_stop_scope(Machine
*m
) {
407 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
412 assert(m
->class != MACHINE_HOST
);
417 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
419 log_error_errno(r
, "Failed to stop machine scope: %s", bus_error_message(&error
, r
));
420 sd_bus_error_free(&error
);
422 free_and_replace(m
->scope_job
, job
);
424 q
= manager_unref_unit(m
->manager
, m
->unit
, &error
);
426 log_warning_errno(q
, "Failed to drop reference to machine scope, ignoring: %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
);
443 (void) manager_enqueue_nscd_cache_flush(m
->manager
);
448 int machine_finalize(Machine
*m
) {
453 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR
,
455 "LEADER="PID_FMT
, m
->leader
,
456 LOG_MESSAGE("Machine %s terminated.", m
->name
));
459 machine_add_to_gc_queue(m
);
462 machine_send_signal(m
, false);
469 bool machine_may_gc(Machine
*m
, bool drop_not_started
) {
472 if (m
->class == MACHINE_HOST
)
475 if (drop_not_started
&& !m
->started
)
478 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
481 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
487 void machine_add_to_gc_queue(Machine
*m
) {
493 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
494 m
->in_gc_queue
= true;
497 MachineState
machine_get_state(Machine
*s
) {
500 if (s
->class == MACHINE_HOST
)
501 return MACHINE_RUNNING
;
504 return MACHINE_CLOSING
;
507 return MACHINE_OPENING
;
509 return MACHINE_RUNNING
;
512 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
515 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
521 if (who
== KILL_LEADER
) {
522 /* If we shall simply kill the leader, do so directly */
524 if (kill(m
->leader
, signo
) < 0)
530 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
531 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
534 int machine_openpt(Machine
*m
, int flags
, char **ret_slave
) {
541 return openpt_allocate(flags
, ret_slave
);
543 case MACHINE_CONTAINER
:
547 return openpt_allocate_in_namespace(m
->leader
, flags
, ret_slave
);
554 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
560 return open_terminal(path
, mode
);
562 case MACHINE_CONTAINER
:
566 return open_terminal_in_namespace(m
->leader
, path
, mode
);
573 void machine_release_unit(Machine
*m
) {
579 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
580 m
->unit
= mfree(m
->unit
);
583 int machine_get_uid_shift(Machine
*m
, uid_t
*ret
) {
584 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
585 uid_t uid_base
, uid_shift
, uid_range
;
586 gid_t gid_base
, gid_shift
, gid_range
;
587 _cleanup_fclose_
FILE *f
= NULL
;
593 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
594 * mappings. In most cases setups should be simple like this, and administrators should only care about the
595 * basic offset a container has relative to the host. This is what this function exposes.
597 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
599 if (m
->class == MACHINE_HOST
) {
604 if (m
->class != MACHINE_CONTAINER
)
607 xsprintf(p
, "/proc/" PID_FMT
"/uid_map", m
->leader
);
610 if (errno
== ENOENT
) {
611 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
619 /* Read the first line. There's at least one. */
621 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", &uid_base
, &uid_shift
, &uid_range
);
624 return errno_or_else(EIO
);
629 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
632 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
633 if (uid_range
< UID_NOBODY
)
636 /* If there's more than one line, then we don't support this mapping. */
637 r
= safe_fgetc(f
, NULL
);
640 if (r
!= 0) /* Insist on EOF */
645 xsprintf(p
, "/proc/" PID_FMT
"/gid_map", m
->leader
);
650 /* Read the first line. There's at least one. */
652 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
"\n", &gid_base
, &gid_shift
, &gid_range
);
655 return errno_or_else(EIO
);
660 /* If there's more than one line, then we don't support this file. */
661 r
= safe_fgetc(f
, NULL
);
664 if (r
!= 0) /* Insist on EOF */
667 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
668 if (uid_base
!= (uid_t
) gid_base
)
670 if (uid_shift
!= (uid_t
) gid_shift
)
672 if (uid_range
!= (uid_t
) gid_range
)
679 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
680 [MACHINE_CONTAINER
] = "container",
682 [MACHINE_HOST
] = "host",
685 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
687 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
688 [MACHINE_OPENING
] = "opening",
689 [MACHINE_RUNNING
] = "running",
690 [MACHINE_CLOSING
] = "closing"
693 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
695 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
696 [KILL_LEADER
] = "leader",
700 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);