1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-26 08:55:40 +03:00

machine: add OSRelease and UIDShift fields in varlink io.systemd.Machine.List output

This commit adds support of the above mentioned fields. This is equivalent to DBus implementation of:
- GetMachineOSRelease
- GetMachineUIDShift
This commit is contained in:
Ivan Kruglov 2024-10-17 13:51:34 +02:00
parent 16b1b304ba
commit a94fbcaa35
2 changed files with 64 additions and 8 deletions

View File

@ -391,14 +391,38 @@ static int vl_method_get_memberships(sd_varlink *link, sd_json_variant *paramete
return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
}
static int list_machine_one(sd_varlink *link, Machine *m, bool more) {
int r;
static int list_machine_one_and_maybe_read_metadata(sd_varlink *link, Machine *m, bool more, AcquireMetadata am) {
_cleanup_strv_free_ char **os_release = NULL;
uid_t shift = UID_INVALID;
int n, r;
assert(link);
assert(m);
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
if (should_acquire_metadata(am)) {
r = machine_get_os_release(m, &os_release);
if (r < 0 && am == ACQUIRE_METADATA_GRACEFUL)
log_debug_errno(r, "Failed to get OS release (graceful mode), ignoring: %m");
else if (r == -ENONET)
return sd_varlink_error(link, "io.systemd.Machine.NoOSReleaseInformation", NULL);
else if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
return sd_varlink_error(link, "io.systemd.Machine.NotAvailable", NULL);
else if (r < 0)
return log_debug_errno(r, "Failed to get OS release: %m");
r = machine_get_uid_shift(m, &shift);
if (r < 0 && am == ACQUIRE_METADATA_GRACEFUL)
log_debug_errno(r, "Failed to get UID shift (graceful mode), ignoring: %m");
else if (r == -ENXIO)
return sd_varlink_error(link, "io.systemd.Machine.NoUIDShift", NULL);
else if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
return sd_varlink_error(link, "io.systemd.Machine.NotAvailable", NULL);
else if (r < 0)
return log_debug_errno(r, "Failed to get UID shift: %m");
}
r = sd_json_buildo(
&v,
SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(m->name)),
@ -411,7 +435,9 @@ static int list_machine_one(sd_varlink *link, Machine *m, bool more) {
SD_JSON_BUILD_PAIR_CONDITION(dual_timestamp_is_set(&m->timestamp), "timestamp", JSON_BUILD_DUAL_TIMESTAMP(&m->timestamp)),
SD_JSON_BUILD_PAIR_CONDITION(m->vsock_cid != VMADDR_CID_ANY, "vSockCid", SD_JSON_BUILD_UNSIGNED(m->vsock_cid)),
JSON_BUILD_PAIR_STRING_NON_EMPTY("sshAddress", m->ssh_address),
JSON_BUILD_PAIR_STRING_NON_EMPTY("sshPrivateKeyPath", m->ssh_private_key_path));
JSON_BUILD_PAIR_STRING_NON_EMPTY("sshPrivateKeyPath", m->ssh_private_key_path),
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(os_release), "OSRelease", JSON_BUILD_STRV_ENV_PAIR(os_release)),
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("UIDShift", shift, UID_INVALID));
if (r < 0)
return r;
@ -424,6 +450,7 @@ static int list_machine_one(sd_varlink *link, Machine *m, bool more) {
typedef struct MachineLookupParameters {
const char *name;
PidRef pidref;
AcquireMetadata acquire_metadata;
} MachineLookupParameters;
static void machine_lookup_parameters_done(MachineLookupParameters *p) {
@ -437,6 +464,7 @@ static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_acquire_metadata, AcquireMetadata
static int vl_method_list(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
static const sd_json_dispatch_field dispatch_table[] = {
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(MachineLookupParameters),
{ "acquireMetadata", SD_JSON_VARIANT_STRING, json_dispatch_acquire_metadata, offsetof(MachineLookupParameters, acquire_metadata), 0 },
VARLINK_DISPATCH_POLKIT_FIELD,
{}
};
@ -444,7 +472,9 @@ static int vl_method_list(sd_varlink *link, sd_json_variant *parameters, sd_varl
Manager *m = ASSERT_PTR(userdata);
_cleanup_(machine_lookup_parameters_done) MachineLookupParameters p = {
.pidref = PIDREF_NULL,
.acquire_metadata = ACQUIRE_METADATA_NO,
};
Machine *machine;
int r;
@ -462,7 +492,7 @@ static int vl_method_list(sd_varlink *link, sd_json_variant *parameters, sd_varl
if (r != 0)
return r;
return list_machine_one(link, machine, /* more= */ false);
return list_machine_one_and_maybe_read_metadata(link, machine, /* more = */ false, p.acquire_metadata);
}
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE))
@ -471,7 +501,7 @@ static int vl_method_list(sd_varlink *link, sd_json_variant *parameters, sd_varl
Machine *previous = NULL, *i;
HASHMAP_FOREACH(i, m->machines) {
if (previous) {
r = list_machine_one(link, previous, /* more= */ true);
r = list_machine_one_and_maybe_read_metadata(link, previous, /* more = */ true, p.acquire_metadata);
if (r < 0)
return r;
}
@ -480,7 +510,7 @@ static int vl_method_list(sd_varlink *link, sd_json_variant *parameters, sd_varl
}
if (previous)
return list_machine_one(link, previous, /* more= */ false);
return list_machine_one_and_maybe_read_metadata(link, previous, /* more = */ false, p.acquire_metadata);
return sd_varlink_error(link, "io.systemd.Machine.NoSuchMachine", NULL);
}

View File

@ -13,6 +13,15 @@
SD_VARLINK_DEFINE_INPUT_BY_TYPE(pid, ProcessId, SD_VARLINK_NULLABLE), \
VARLINK_DEFINE_POLKIT_INPUT
static SD_VARLINK_DEFINE_ENUM_TYPE(
AcquireMetadata,
SD_VARLINK_FIELD_COMMENT("Do not include metadata in the output"),
SD_VARLINK_DEFINE_ENUM_VALUE(no),
SD_VARLINK_FIELD_COMMENT("Include metadata in the output"),
SD_VARLINK_DEFINE_ENUM_VALUE(yes),
SD_VARLINK_FIELD_COMMENT("Include metadata in the output, but gracefully eat up errors"),
SD_VARLINK_DEFINE_ENUM_VALUE(graceful));
static SD_VARLINK_DEFINE_METHOD(
Register,
SD_VARLINK_DEFINE_INPUT(name, SD_VARLINK_STRING, 0),
@ -49,6 +58,8 @@ static SD_VARLINK_DEFINE_METHOD_FULL(
List,
SD_VARLINK_SUPPORTS_MORE,
VARLINK_DEFINE_MACHINE_LOOKUP_AND_POLKIT_INPUT_FIELDS,
SD_VARLINK_FIELD_COMMENT("If 'yes' the output will include machine metadata fields such as 'OSRelease' and 'UIDShift'. If 'graceful' it's equal to true but gracefully eats up errors"),
SD_VARLINK_DEFINE_INPUT_BY_TYPE(acquireMetadata, AcquireMetadata, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("Name of the machine"),
SD_VARLINK_DEFINE_OUTPUT(name, SD_VARLINK_STRING, 0),
SD_VARLINK_FIELD_COMMENT("128bit ID identifying this machine, formatted in hexadecimal"),
@ -70,10 +81,17 @@ static SD_VARLINK_DEFINE_METHOD_FULL(
SD_VARLINK_FIELD_COMMENT("SSH address to connect to"),
SD_VARLINK_DEFINE_OUTPUT(sshAddress, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("Path to private SSH key"),
SD_VARLINK_DEFINE_OUTPUT(sshPrivateKeyPath, SD_VARLINK_STRING, SD_VARLINK_NULLABLE));
SD_VARLINK_DEFINE_OUTPUT(sshPrivateKeyPath, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
SD_VARLINK_FIELD_COMMENT("OS release information of the machine. It contains an array of key value pairs read from the os-release(5) file in the image."),
SD_VARLINK_DEFINE_OUTPUT(OSRelease, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY),
SD_VARLINK_FIELD_COMMENT("Return the base UID/GID of the machine"),
SD_VARLINK_DEFINE_OUTPUT(UIDShift, SD_VARLINK_INT, SD_VARLINK_NULLABLE));
static SD_VARLINK_DEFINE_ERROR(NoSuchMachine);
static SD_VARLINK_DEFINE_ERROR(MachineExists);
static SD_VARLINK_DEFINE_ERROR(NoOSReleaseInformation);
static SD_VARLINK_DEFINE_ERROR(NoUIDShift);
static SD_VARLINK_DEFINE_ERROR(NotAvailable);
SD_VARLINK_DEFINE_INTERFACE(
io_systemd_Machine,
@ -82,6 +100,8 @@ SD_VARLINK_DEFINE_INTERFACE(
&vl_type_ProcessId,
SD_VARLINK_SYMBOL_COMMENT("A timestamp object consisting of both CLOCK_REALTIME and CLOCK_MONOTONIC timestamps"),
&vl_type_Timestamp,
SD_VARLINK_SYMBOL_COMMENT("A enum field allowing to gracefully get metadata"),
&vl_type_AcquireMetadata,
&vl_method_Register,
&vl_method_Unregister,
SD_VARLINK_SYMBOL_COMMENT("Terminate machine, killing its processes"),
@ -92,4 +112,10 @@ SD_VARLINK_DEFINE_INTERFACE(
&vl_method_List,
SD_VARLINK_SYMBOL_COMMENT("No matching machine currently running"),
&vl_error_NoSuchMachine,
&vl_error_MachineExists);
&vl_error_MachineExists,
SD_VARLINK_SYMBOL_COMMENT("Machine does not contain OS release information"),
&vl_error_NoOSReleaseInformation,
SD_VARLINK_SYMBOL_COMMENT("Machine uses a complex UID/GID mapping, cannot determine shift"),
&vl_error_NoUIDShift,
SD_VARLINK_SYMBOL_COMMENT("Requested information is not available"),
&vl_error_NotAvailable);