core: implement ScopeContext/Runtime for io.systemd.Unit.List + tests
Add varlink context and runtime builders for .scope units: ScopeContext: OOMPolicy (enum), RuntimeMaxUSec, RuntimeRandomizedExtraUSec, TimeoutStopUSec ScopeRuntime: Result (ScopeResult enum) Both OOMPolicy and ScopeResult are exposed as proper varlink enum types. Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -74,6 +74,7 @@ libcore_sources = files(
|
||||
'varlink-metrics.c',
|
||||
'varlink-mount.c',
|
||||
'varlink-path.c',
|
||||
'varlink-scope.c',
|
||||
'varlink-unit.c',
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "sd-json.h"
|
||||
|
||||
#include "json-util.h"
|
||||
#include "scope.h"
|
||||
#include "varlink-scope.h"
|
||||
|
||||
int scope_context_build_json(sd_json_variant **ret, const char *name, void *userdata) {
|
||||
Scope *s = ASSERT_PTR(SCOPE(userdata));
|
||||
|
||||
return sd_json_buildo(
|
||||
ASSERT_PTR(ret),
|
||||
JSON_BUILD_PAIR_ENUM("OOMPolicy", oom_policy_to_string(s->oom_policy)),
|
||||
JSON_BUILD_PAIR_FINITE_USEC("RuntimeMaxUSec", s->runtime_max_usec),
|
||||
JSON_BUILD_PAIR_FINITE_USEC("RuntimeRandomizedExtraUSec", s->runtime_rand_extra_usec),
|
||||
JSON_BUILD_PAIR_FINITE_USEC("TimeoutStopUSec", s->timeout_stop_usec));
|
||||
}
|
||||
|
||||
int scope_runtime_build_json(sd_json_variant **ret, const char *name, void *userdata) {
|
||||
Scope *s = ASSERT_PTR(SCOPE(userdata));
|
||||
|
||||
return sd_json_buildo(
|
||||
ASSERT_PTR(ret),
|
||||
JSON_BUILD_PAIR_ENUM("Result", scope_result_to_string(s->result)));
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include "core-forward.h"
|
||||
|
||||
int scope_context_build_json(sd_json_variant **ret, const char *name, void *userdata);
|
||||
int scope_runtime_build_json(sd_json_variant **ret, const char *name, void *userdata);
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "varlink-kill.h"
|
||||
#include "varlink-mount.h"
|
||||
#include "varlink-path.h"
|
||||
#include "varlink-scope.h"
|
||||
#include "varlink-unit.h"
|
||||
#include "varlink-util.h"
|
||||
|
||||
@@ -164,6 +165,7 @@ static int unit_context_build_json(sd_json_variant **ret, const char *name, void
|
||||
[UNIT_AUTOMOUNT] = automount_context_build_json,
|
||||
[UNIT_MOUNT] = mount_context_build_json,
|
||||
[UNIT_PATH] = path_context_build_json,
|
||||
[UNIT_SCOPE] = scope_context_build_json,
|
||||
[UNIT_SERVICE] = service_context_build_json,
|
||||
};
|
||||
|
||||
@@ -331,6 +333,7 @@ static int unit_runtime_build_json(sd_json_variant **ret, const char *name, void
|
||||
[UNIT_AUTOMOUNT] = automount_runtime_build_json,
|
||||
[UNIT_MOUNT] = mount_runtime_build_json,
|
||||
[UNIT_PATH] = path_runtime_build_json,
|
||||
[UNIT_SCOPE] = scope_runtime_build_json,
|
||||
};
|
||||
|
||||
return sd_json_buildo(
|
||||
|
||||
@@ -1016,6 +1016,31 @@ static SD_VARLINK_DEFINE_STRUCT_TYPE(
|
||||
SD_VARLINK_FIELD_COMMENT("Result of path operation"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Result, PathResult, 0));
|
||||
|
||||
/* ScopeContext
|
||||
* https://www.freedesktop.org/software/systemd/man/latest/systemd.scope.html */
|
||||
static SD_VARLINK_DEFINE_STRUCT_TYPE(
|
||||
ScopeContext,
|
||||
SD_VARLINK_FIELD_COMMENT("https://www.freedesktop.org/software/systemd/man/"PROJECT_VERSION_STR"/systemd.scope.html#OOMPolicy="),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(OOMPolicy, OOMPolicy, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("https://www.freedesktop.org/software/systemd/man/"PROJECT_VERSION_STR"/systemd.scope.html#RuntimeMaxSec="),
|
||||
SD_VARLINK_DEFINE_FIELD(RuntimeMaxUSec, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("https://www.freedesktop.org/software/systemd/man/"PROJECT_VERSION_STR"/systemd.scope.html#RuntimeRandomizedExtraSec="),
|
||||
SD_VARLINK_DEFINE_FIELD(RuntimeRandomizedExtraUSec, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("https://www.freedesktop.org/software/systemd/man/"PROJECT_VERSION_STR"/systemd.scope.html#TimeoutStopSec="),
|
||||
SD_VARLINK_DEFINE_FIELD(TimeoutStopUSec, SD_VARLINK_INT, SD_VARLINK_NULLABLE));
|
||||
|
||||
SD_VARLINK_DEFINE_ENUM_TYPE(
|
||||
ScopeResult,
|
||||
SD_VARLINK_DEFINE_ENUM_VALUE(success),
|
||||
SD_VARLINK_DEFINE_ENUM_VALUE(resources),
|
||||
SD_VARLINK_DEFINE_ENUM_VALUE(timeout),
|
||||
SD_VARLINK_DEFINE_ENUM_VALUE(oom_kill));
|
||||
|
||||
static SD_VARLINK_DEFINE_STRUCT_TYPE(
|
||||
ScopeRuntime,
|
||||
SD_VARLINK_FIELD_COMMENT("Result of scope operation"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Result, ScopeResult, 0));
|
||||
|
||||
/* Service-specific types */
|
||||
|
||||
/* Keep in sync with service_type_table[] in src/core/service.c */
|
||||
@@ -1210,7 +1235,9 @@ static SD_VARLINK_DEFINE_STRUCT_TYPE(
|
||||
SD_VARLINK_FIELD_COMMENT("The mount context of the unit"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Mount, MountContext, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("The path context of the unit"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Path, PathContext, SD_VARLINK_NULLABLE));
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Path, PathContext, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("The scope context of the unit"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Scope, ScopeContext, SD_VARLINK_NULLABLE));
|
||||
|
||||
static SD_VARLINK_DEFINE_STRUCT_TYPE(
|
||||
ActivationDetails,
|
||||
@@ -1386,7 +1413,9 @@ static SD_VARLINK_DEFINE_STRUCT_TYPE(
|
||||
SD_VARLINK_FIELD_COMMENT("The mount runtime of the unit"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Mount, MountRuntime, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("The path runtime of the unit"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Path, PathRuntime, SD_VARLINK_NULLABLE));
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Path, PathRuntime, SD_VARLINK_NULLABLE),
|
||||
SD_VARLINK_FIELD_COMMENT("The scope runtime of the unit"),
|
||||
SD_VARLINK_DEFINE_FIELD_BY_TYPE(Scope, ScopeRuntime, SD_VARLINK_NULLABLE));
|
||||
|
||||
static SD_VARLINK_DEFINE_METHOD_FULL(
|
||||
List,
|
||||
@@ -1553,6 +1582,10 @@ SD_VARLINK_DEFINE_INTERFACE(
|
||||
&vl_type_PathContext,
|
||||
&vl_type_PathResult,
|
||||
&vl_type_PathRuntime,
|
||||
&vl_type_OOMPolicy,
|
||||
&vl_type_ScopeContext,
|
||||
&vl_type_ScopeResult,
|
||||
&vl_type_ScopeRuntime,
|
||||
|
||||
/* UnitContext enums */
|
||||
&vl_type_CollectMode,
|
||||
|
||||
@@ -32,6 +32,7 @@ extern const sd_varlink_symbol vl_type_AutomountResult;
|
||||
extern const sd_varlink_symbol vl_type_MountResult;
|
||||
extern const sd_varlink_symbol vl_type_PathType;
|
||||
extern const sd_varlink_symbol vl_type_PathResult;
|
||||
extern const sd_varlink_symbol vl_type_ScopeResult;
|
||||
extern const sd_varlink_symbol vl_type_CollectMode;
|
||||
extern const sd_varlink_symbol vl_type_JobMode;
|
||||
extern const sd_varlink_symbol vl_type_ServiceType;
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
#include "numa-util.h"
|
||||
#include "path.h"
|
||||
#include "process-util.h"
|
||||
#include "scope.h"
|
||||
#include "service.h"
|
||||
#include "swap.h"
|
||||
#include "tests.h"
|
||||
#include "test-varlink-idl-util.h"
|
||||
#include "unit.h"
|
||||
@@ -70,6 +72,9 @@ TEST(unit_enums_idl) {
|
||||
/* PathRuntime enums */
|
||||
TEST_IDL_ENUM(PathResult, path_result, vl_type_PathResult);
|
||||
|
||||
/* ScopeRuntime enums */
|
||||
TEST_IDL_ENUM(ScopeResult, scope_result, vl_type_ScopeResult);
|
||||
|
||||
/* UnitContext enums */
|
||||
TEST_IDL_ENUM(CollectMode, collect_mode, vl_type_CollectMode);
|
||||
TEST_IDL_ENUM(EmergencyAction, emergency_action, vl_type_EmergencyAction);
|
||||
|
||||
@@ -252,6 +252,12 @@ test -n "$path_id"
|
||||
path_params=$(jq -cn --arg name "$path_id" '{name: $name}')
|
||||
varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List "$path_params" | jq -e '.context.Path'
|
||||
varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List "$path_params" | jq -e '.runtime.Path'
|
||||
# test for ScopeContext/Runtime
|
||||
scope_id=$(varlinkctl call --collect /run/systemd/io.systemd.Manager io.systemd.Unit.List '{}' | jq -r '.[] | select(.context.Type == "scope" and .runtime.LoadState == "loaded") .context.ID' | grep -v null | tail -n 1)
|
||||
test -n "$scope_id"
|
||||
scope_params=$(jq -cn --arg name "$scope_id" '{name: $name}')
|
||||
varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List "$scope_params" | jq -e '.context.Scope'
|
||||
varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List "$scope_params" | jq -e '.runtime.Scope'
|
||||
|
||||
# test io.systemd.Metrics
|
||||
varlinkctl info /run/systemd/report/io.systemd.Manager
|
||||
|
||||
Reference in New Issue
Block a user