2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "sd-messages.h"
26 #include "alloc-util.h"
27 #include "bus-error.h"
30 #include "extract-word.h"
33 #include "format-util.h"
35 #include "machine-dbus.h"
38 #include "parse-util.h"
39 #include "process-util.h"
41 #include "stdio-util.h"
42 #include "string-table.h"
43 #include "terminal-util.h"
44 #include "unit-name.h"
47 Machine
* machine_new(Manager
*manager
, MachineClass
class, const char *name
) {
51 assert(class < _MACHINE_CLASS_MAX
);
54 /* Passing class == _MACHINE_CLASS_INVALID here is fine. It
55 * means as much as "we don't know yet", and that we'll figure
56 * it out later when loading the state file. */
62 m
->name
= strdup(name
);
66 if (class != MACHINE_HOST
) {
67 m
->state_file
= strappend("/run/systemd/machines/", m
->name
);
74 if (hashmap_put(manager
->machines
, m
->name
, m
) < 0)
87 void machine_free(Machine
*m
) {
91 operation_free(m
->operations
);
94 LIST_REMOVE(gc_queue
, m
->manager
->machine_gc_queue
, m
);
96 machine_release_unit(m
);
100 (void) hashmap_remove(m
->manager
->machines
, m
->name
);
102 if (m
->manager
->host_machine
== m
)
103 m
->manager
->host_machine
= NULL
;
106 (void) hashmap_remove_value(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
108 sd_bus_message_unref(m
->create_message
);
113 free(m
->root_directory
);
118 int machine_save(Machine
*m
) {
119 _cleanup_free_
char *temp_path
= NULL
;
120 _cleanup_fclose_
FILE *f
= NULL
;
131 r
= mkdir_safe_label("/run/systemd/machines", 0755, 0, 0);
135 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
139 (void) fchmod(fileno(f
), 0644);
142 "# This is private data. Do not parse.\n"
147 _cleanup_free_
char *escaped
;
149 escaped
= cescape(m
->unit
);
155 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 */
159 fprintf(f
, "SCOPE_JOB=%s\n", m
->scope_job
);
162 _cleanup_free_
char *escaped
;
164 escaped
= cescape(m
->service
);
169 fprintf(f
, "SERVICE=%s\n", escaped
);
172 if (m
->root_directory
) {
173 _cleanup_free_
char *escaped
;
175 escaped
= cescape(m
->root_directory
);
180 fprintf(f
, "ROOT=%s\n", escaped
);
183 if (!sd_id128_is_null(m
->id
))
184 fprintf(f
, "ID=" SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->id
));
187 fprintf(f
, "LEADER="PID_FMT
"\n", m
->leader
);
189 if (m
->class != _MACHINE_CLASS_INVALID
)
190 fprintf(f
, "CLASS=%s\n", machine_class_to_string(m
->class));
192 if (dual_timestamp_is_set(&m
->timestamp
))
194 "REALTIME="USEC_FMT
"\n"
195 "MONOTONIC="USEC_FMT
"\n",
196 m
->timestamp
.realtime
,
197 m
->timestamp
.monotonic
);
199 if (m
->n_netif
> 0) {
202 fputs_unlocked("NETIF=", f
);
204 for (i
= 0; i
< m
->n_netif
; i
++) {
206 fputc_unlocked(' ', f
);
208 fprintf(f
, "%i", m
->netif
[i
]);
211 fputc_unlocked('\n', f
);
214 r
= fflush_and_check(f
);
218 if (rename(temp_path
, m
->state_file
) < 0) {
226 /* Create a symlink from the unit name to the machine
227 * name, so that we can quickly find the machine for
228 * each given unit. Ignore error. */
229 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
230 (void) symlink(m
->name
, sl
);
236 (void) unlink(m
->state_file
);
239 (void) unlink(temp_path
);
241 return log_error_errno(r
, "Failed to save machine data %s: %m", m
->state_file
);
244 static void machine_unlink(Machine
*m
) {
251 sl
= strjoina("/run/systemd/machines/unit:", m
->unit
);
256 (void) unlink(m
->state_file
);
259 int machine_load(Machine
*m
) {
260 _cleanup_free_
char *realtime
= NULL
, *monotonic
= NULL
, *id
= NULL
, *leader
= NULL
, *class = NULL
, *netif
= NULL
;
268 r
= parse_env_file(m
->state_file
, NEWLINE
,
270 "SCOPE_JOB", &m
->scope_job
,
271 "SERVICE", &m
->service
,
272 "ROOT", &m
->root_directory
,
276 "REALTIME", &realtime
,
277 "MONOTONIC", &monotonic
,
284 return log_error_errno(r
, "Failed to read %s: %m", m
->state_file
);
288 sd_id128_from_string(id
, &m
->id
);
291 parse_pid(leader
, &m
->leader
);
296 c
= machine_class_from_string(class);
302 timestamp_deserialize(realtime
, &m
->timestamp
.realtime
);
304 timestamp_deserialize(monotonic
, &m
->timestamp
.monotonic
);
307 size_t allocated
= 0, nr
= 0;
313 _cleanup_free_
char *word
= NULL
;
316 r
= extract_first_word(&p
, &word
, NULL
, 0);
322 log_warning_errno(r
, "Failed to parse NETIF: %s", netif
);
326 if (parse_ifindex(word
, &ifi
) < 0)
329 if (!GREEDY_REALLOC(ni
, allocated
, nr
+1)) {
345 static int machine_start_scope(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
349 assert(m
->class != MACHINE_HOST
);
352 _cleanup_free_
char *escaped
= NULL
;
353 char *scope
, *description
, *job
= NULL
;
355 escaped
= unit_name_escape(m
->name
);
359 scope
= strjoin("machine-", escaped
, ".scope");
363 description
= strjoina(m
->class == MACHINE_VM
? "Virtual Machine " : "Container ", m
->name
);
365 r
= manager_start_scope(m
->manager
, scope
, m
->leader
, SPECIAL_MACHINE_SLICE
, description
, properties
, error
, &job
);
367 log_error("Failed to start machine scope: %s", bus_error_message(error
, r
));
379 hashmap_put(m
->manager
->machine_units
, m
->unit
, m
);
384 int machine_start(Machine
*m
, sd_bus_message
*properties
, sd_bus_error
*error
) {
389 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
395 r
= hashmap_put(m
->manager
->machine_leaders
, PID_TO_PTR(m
->leader
), m
);
400 r
= machine_start_scope(m
, properties
, error
);
405 "MESSAGE_ID=" SD_MESSAGE_MACHINE_START_STR
,
407 "LEADER="PID_FMT
, m
->leader
,
408 LOG_MESSAGE("New machine %s.", m
->name
),
411 if (!dual_timestamp_is_set(&m
->timestamp
))
412 dual_timestamp_get(&m
->timestamp
);
416 /* Save new machine data */
419 machine_send_signal(m
, true);
424 static int machine_stop_scope(Machine
*m
) {
425 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
430 assert(m
->class != MACHINE_HOST
);
435 r
= manager_stop_unit(m
->manager
, m
->unit
, &error
, &job
);
437 log_error("Failed to stop machine scope: %s", bus_error_message(&error
, r
));
447 int machine_stop(Machine
*m
) {
451 if (!IN_SET(m
->class, MACHINE_CONTAINER
, MACHINE_VM
))
454 r
= machine_stop_scope(m
);
463 int machine_finalize(Machine
*m
) {
468 "MESSAGE_ID=" SD_MESSAGE_MACHINE_STOP_STR
,
470 "LEADER="PID_FMT
, m
->leader
,
471 LOG_MESSAGE("Machine %s terminated.", m
->name
),
475 machine_add_to_gc_queue(m
);
478 machine_send_signal(m
, false);
485 bool machine_check_gc(Machine
*m
, bool drop_not_started
) {
488 if (m
->class == MACHINE_HOST
)
491 if (drop_not_started
&& !m
->started
)
494 if (m
->scope_job
&& manager_job_is_active(m
->manager
, m
->scope_job
))
497 if (m
->unit
&& manager_unit_is_active(m
->manager
, m
->unit
))
503 void machine_add_to_gc_queue(Machine
*m
) {
509 LIST_PREPEND(gc_queue
, m
->manager
->machine_gc_queue
, m
);
510 m
->in_gc_queue
= true;
513 MachineState
machine_get_state(Machine
*s
) {
516 if (s
->class == MACHINE_HOST
)
517 return MACHINE_RUNNING
;
520 return MACHINE_CLOSING
;
523 return MACHINE_OPENING
;
525 return MACHINE_RUNNING
;
528 int machine_kill(Machine
*m
, KillWho who
, int signo
) {
531 if (!IN_SET(m
->class, MACHINE_VM
, MACHINE_CONTAINER
))
537 if (who
== KILL_LEADER
) {
538 /* If we shall simply kill the leader, do so directly */
540 if (kill(m
->leader
, signo
) < 0)
546 /* Otherwise, make PID 1 do it for us, for the entire cgroup */
547 return manager_kill_unit(m
->manager
, m
->unit
, signo
, NULL
);
550 int machine_openpt(Machine
*m
, int flags
) {
558 fd
= posix_openpt(flags
);
562 if (unlockpt(fd
) < 0)
568 case MACHINE_CONTAINER
:
572 return openpt_in_namespace(m
->leader
, flags
);
579 int machine_open_terminal(Machine
*m
, const char *path
, int mode
) {
585 return open_terminal(path
, mode
);
587 case MACHINE_CONTAINER
:
591 return open_terminal_in_namespace(m
->leader
, path
, mode
);
598 void machine_release_unit(Machine
*m
) {
604 (void) hashmap_remove(m
->manager
->machine_units
, m
->unit
);
605 m
->unit
= mfree(m
->unit
);
608 int machine_get_uid_shift(Machine
*m
, uid_t
*ret
) {
609 char p
[strlen("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
610 uid_t uid_base
, uid_shift
, uid_range
;
611 gid_t gid_base
, gid_shift
, gid_range
;
612 _cleanup_fclose_
FILE *f
= NULL
;
618 /* Return the base UID/GID of the specified machine. Note that this only works for containers with simple
619 * mappings. In most cases setups should be simple like this, and administrators should only care about the
620 * basic offset a container has relative to the host. This is what this function exposes.
622 * If we encounter any more complex mappings we politely refuse this with ENXIO. */
624 if (m
->class == MACHINE_HOST
) {
629 if (m
->class != MACHINE_CONTAINER
)
632 xsprintf(p
, "/proc/" PID_FMT
"/uid_map", m
->leader
);
635 if (errno
== ENOENT
) {
636 /* If the file doesn't exist, user namespacing is off in the kernel, return a zero mapping hence. */
644 /* Read the first line. There's at least one. */
646 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", &uid_base
, &uid_shift
, &uid_range
);
654 /* Not a mapping starting at 0? Then it's a complex mapping we can't expose here. */
657 /* Insist that at least the nobody user is mapped, everything else is weird, and hence complex, and we don't support it */
658 if (uid_range
< (uid_t
) 65534U)
661 /* If there's more than one line, then we don't support this mapping. */
667 xsprintf(p
, "/proc/" PID_FMT
"/gid_map", m
->leader
);
672 /* Read the first line. There's at least one. */
674 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
"\n", &gid_base
, &gid_shift
, &gid_range
);
682 /* If there's more than one line, then we don't support this file. */
686 /* If the UID and GID mapping doesn't match, we don't support this mapping. */
687 if (uid_base
!= (uid_t
) gid_base
)
689 if (uid_shift
!= (uid_t
) gid_shift
)
691 if (uid_range
!= (uid_t
) gid_range
)
698 static const char* const machine_class_table
[_MACHINE_CLASS_MAX
] = {
699 [MACHINE_CONTAINER
] = "container",
701 [MACHINE_HOST
] = "host",
704 DEFINE_STRING_TABLE_LOOKUP(machine_class
, MachineClass
);
706 static const char* const machine_state_table
[_MACHINE_STATE_MAX
] = {
707 [MACHINE_OPENING
] = "opening",
708 [MACHINE_RUNNING
] = "running",
709 [MACHINE_CLOSING
] = "closing"
712 DEFINE_STRING_TABLE_LOOKUP(machine_state
, MachineState
);
714 static const char* const kill_who_table
[_KILL_WHO_MAX
] = {
715 [KILL_LEADER
] = "leader",
719 DEFINE_STRING_TABLE_LOOKUP(kill_who
, KillWho
);