From: Ivan Kruglov Date: Thu, 7 May 2026 12:02:01 +0000 (-0700) Subject: core: implement ScopeContext/Runtime for io.systemd.Unit.List + tests X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=412b2d310137b46d7273ab76ef79f8767704c850;p=thirdparty%2Fsystemd.git 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 --- diff --git a/src/core/meson.build b/src/core/meson.build index 0dae1e50cc9..796669814bf 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -74,6 +74,7 @@ libcore_sources = files( 'varlink-metrics.c', 'varlink-mount.c', 'varlink-path.c', + 'varlink-scope.c', 'varlink-unit.c', ) diff --git a/src/core/varlink-scope.c b/src/core/varlink-scope.c new file mode 100644 index 00000000000..d22a2da5701 --- /dev/null +++ b/src/core/varlink-scope.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))); +} diff --git a/src/core/varlink-scope.h b/src/core/varlink-scope.h new file mode 100644 index 00000000000..0e941f873dd --- /dev/null +++ b/src/core/varlink-scope.h @@ -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); diff --git a/src/core/varlink-unit.c b/src/core/varlink-unit.c index ca357cf899e..30b912ddf10 100644 --- a/src/core/varlink-unit.c +++ b/src/core/varlink-unit.c @@ -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( diff --git a/src/shared/varlink-io.systemd.Unit.c b/src/shared/varlink-io.systemd.Unit.c index 4e8e4f04b7f..83a728e2cc6 100644 --- a/src/shared/varlink-io.systemd.Unit.c +++ b/src/shared/varlink-io.systemd.Unit.c @@ -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, diff --git a/src/shared/varlink-io.systemd.Unit.h b/src/shared/varlink-io.systemd.Unit.h index bc43d7b59fb..54d2dad1c13 100644 --- a/src/shared/varlink-io.systemd.Unit.h +++ b/src/shared/varlink-io.systemd.Unit.h @@ -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; diff --git a/src/test/test-varlink-idl-unit.c b/src/test/test-varlink-idl-unit.c index 6c0f834a7a3..5e9ce4ce061 100644 --- a/src/test/test-varlink-idl-unit.c +++ b/src/test/test-varlink-idl-unit.c @@ -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); diff --git a/test/units/TEST-74-AUX-UTILS.varlinkctl.sh b/test/units/TEST-74-AUX-UTILS.varlinkctl.sh index 1db22b85c11..900eda1138c 100755 --- a/test/units/TEST-74-AUX-UTILS.varlinkctl.sh +++ b/test/units/TEST-74-AUX-UTILS.varlinkctl.sh @@ -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