| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "sd-id128.h" |
| 5 |
|
| 6 |
#include "copy.h" |
| 7 |
#include "list.h" |
| 8 |
#include "machine-forward.h" |
| 9 |
#include "pidref.h" |
| 10 |
#include "time-util.h" |
| 11 |
|
| 12 |
typedef enum MachineState { |
| 13 |
MACHINE_OPENING, /* Machine is being registered */ |
| 14 |
MACHINE_RUNNING, /* Machine is running */ |
| 15 |
MACHINE_CLOSING, /* Machine is terminating */ |
| 16 |
_MACHINE_STATE_MAX, |
| 17 |
_MACHINE_STATE_INVALID = -EINVAL, |
| 18 |
} MachineState; |
| 19 |
|
| 20 |
typedef enum MachineClass { |
| 21 |
MACHINE_CONTAINER, |
| 22 |
MACHINE_VM, |
| 23 |
MACHINE_HOST, |
| 24 |
_MACHINE_CLASS_MAX, |
| 25 |
_MACHINE_CLASS_INVALID = -EINVAL, |
| 26 |
} MachineClass; |
| 27 |
|
| 28 |
#define MACHINE_CLASS_CAN_REGISTER(class) IN_SET((class), MACHINE_CONTAINER, MACHINE_VM) |
| 29 |
|
| 30 |
typedef enum KillWhom { |
| 31 |
KILL_LEADER, |
| 32 |
KILL_SUPERVISOR, |
| 33 |
KILL_ALL, |
| 34 |
_KILL_WHOM_MAX, |
| 35 |
_KILL_WHOM_INVALID = -EINVAL, |
| 36 |
} KillWhom; |
| 37 |
|
| 38 |
typedef struct Machine { |
| 39 |
/* Note: machine objects registered with the --system instance can be allocated by privileged *and* |
| 40 |
* unprivileged clients. We generally do this to make DNS-style name resolution work, and since |
| 41 |
* that's a system-wide concept, the machine registrations need to be system-wide too. |
| 42 |
* |
| 43 |
* polkit manages access to machines registered by unprivileged clients. The general rule should be |
| 44 |
* that local users (i.e. those with a seat) may register machines, and do basic interaction with |
| 45 |
* their own machines without having to authenticate as administrator – however any more complex |
| 46 |
* (such as: copying files in + out of a container; or logging in interactively) should only be |
| 47 |
* available after administrator authentication, following the logic that users better use their own |
| 48 |
* per-user instance of systemd-machined for that. */ |
| 49 |
|
| 50 |
Manager *manager; |
| 51 |
|
| 52 |
char *name; |
| 53 |
sd_id128_t id; |
| 54 |
|
| 55 |
uid_t uid; |
| 56 |
|
| 57 |
MachineClass class; |
| 58 |
|
| 59 |
char *state_file; |
| 60 |
char *service; |
| 61 |
/* Note that the root directory is accepted as-is from the caller, including unprivileged users, so |
| 62 |
* do not use it for anything but informational purposes. */ |
| 63 |
char *root_directory; |
| 64 |
|
| 65 |
char *unit; |
| 66 |
char *subgroup; |
| 67 |
char *scope_job; |
| 68 |
char *cgroup; |
| 69 |
|
| 70 |
/* Leader: the top-level process that encapsulates the machine itself. For containers that's PID 1, |
| 71 |
* for VMs that's qemu or whatever process wraps the actual VM code. This process defines the runtime |
| 72 |
* lifecycle of the machine. In case of containers we can use this reference to enter namespaces, |
| 73 |
* send signals and so on. |
| 74 |
* |
| 75 |
* Supervisor: the process that supervises the machine, if there is any and if that process is |
| 76 |
* responsible for a single machine. Sending SIGTERM to this process should (non-cooperatively) |
| 77 |
* terminate the machine. */ |
| 78 |
PidRef leader, supervisor; |
| 79 |
sd_event_source *leader_pidfd_event_source, *supervisor_pidfd_event_source; |
| 80 |
|
| 81 |
dual_timestamp timestamp; |
| 82 |
|
| 83 |
sd_event_source *cgroup_empty_event_source; |
| 84 |
|
| 85 |
bool in_gc_queue:1; |
| 86 |
bool started:1; |
| 87 |
bool stopping:1; |
| 88 |
bool referenced:1; |
| 89 |
bool allocate_unit; |
| 90 |
|
| 91 |
sd_bus_message *create_message; |
| 92 |
|
| 93 |
int *netif; |
| 94 |
size_t n_netif; |
| 95 |
|
| 96 |
unsigned vsock_cid; |
| 97 |
char *ssh_address; |
| 98 |
char *ssh_private_key_path; |
| 99 |
char *control_address; |
| 100 |
|
| 101 |
LIST_HEAD(Operation, operations); |
| 102 |
|
| 103 |
LIST_FIELDS(Machine, gc_queue); |
| 104 |
} Machine; |
| 105 |
|
| 106 |
int machine_new(MachineClass class, const char *name, Machine **ret); |
| 107 |
int machine_link(Manager *manager, Machine *machine); |
| 108 |
Machine* machine_free(Machine *m); |
| 109 |
bool machine_may_gc(Machine *m, bool drop_not_started); |
| 110 |
void machine_add_to_gc_queue(Machine *m); |
| 111 |
int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error); |
| 112 |
int machine_stop(Machine *m); |
| 113 |
int machine_finalize(Machine *m); |
| 114 |
int machine_save(Machine *m); |
| 115 |
int machine_load(Machine *m); |
| 116 |
int machine_kill(Machine *m, KillWhom whom, int signo); |
| 117 |
|
| 118 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Machine*, machine_free); |
| 119 |
|
| 120 |
void machine_release_unit(Machine *m); |
| 121 |
|
| 122 |
MachineState machine_get_state(Machine *m); |
| 123 |
|
| 124 |
DECLARE_STRING_TABLE_LOOKUP(machine_class, MachineClass); |
| 125 |
|
| 126 |
DECLARE_STRING_TABLE_LOOKUP(machine_state, MachineState); |
| 127 |
|
| 128 |
DECLARE_STRING_TABLE_LOOKUP(kill_whom, KillWhom); |
| 129 |
|
| 130 |
int machine_openpt(Machine *m, int flags, char **ret_peer); |
| 131 |
int machine_start_getty(Machine *m, const char *ptmx_name, sd_bus_error *error); |
| 132 |
int machine_start_shell(Machine *m, int ptmx_fd, const char *ptmx_name, const char *user, const char *path, char **args, char **env, sd_bus_error *error); |
| 133 |
#define machine_default_shell_path() ("/bin/sh") |
| 134 |
char** machine_default_shell_args(const char *user); |
| 135 |
|
| 136 |
int machine_copy_from_to_operation( |
| 137 |
Manager *manager, |
| 138 |
Machine *machine, |
| 139 |
const char *host_path, |
| 140 |
const char *container_path, |
| 141 |
bool copy_from_container, |
| 142 |
CopyFlags copy_flags, |
| 143 |
Operation **ret); |
| 144 |
|
| 145 |
int machine_get_uid_shift(Machine *m, uid_t *ret); |
| 146 |
|
| 147 |
int machine_owns_uid(Machine *m, uid_t host_uid, uid_t *ret_internal_uid); |
| 148 |
int machine_owns_gid(Machine *m, gid_t host_gid, gid_t *ret_internal_gid); |
| 149 |
|
| 150 |
int machine_translate_uid(Machine *m, uid_t internal_uid, uid_t *ret_host_uid); |
| 151 |
int machine_translate_gid(Machine *m, gid_t internal_gid, gid_t *ret_host_gid); |
| 152 |
|
| 153 |
int machine_open_root_directory(Machine *machine); |
| 154 |
|
| 155 |
typedef enum AcquireMetadata { |
| 156 |
ACQUIRE_METADATA_NO, |
| 157 |
ACQUIRE_METADATA_YES, |
| 158 |
ACQUIRE_METADATA_GRACEFUL, |
| 159 |
_ACQUIRE_METADATA_MAX, |
| 160 |
_ACQUIRE_METADATA_INVALID = -EINVAL, |
| 161 |
} AcquireMetadata; |
| 162 |
|
| 163 |
DECLARE_STRING_TABLE_LOOKUP(acquire_metadata, AcquireMetadata); |
| 164 |
inline static bool should_acquire_metadata(AcquireMetadata am) { |
| 165 |
return am == ACQUIRE_METADATA_YES || am == ACQUIRE_METADATA_GRACEFUL; |
| 166 |
} |