]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: implement ScopeContext/Runtime for io.systemd.Unit.List + tests
authorIvan Kruglov <mail@ikruglov.com>
Thu, 7 May 2026 12:02:01 +0000 (05:02 -0700)
committerIvan Kruglov <mail@ikruglov.com>
Mon, 11 May 2026 11:32:03 +0000 (04:32 -0700)
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>
src/core/meson.build
src/core/varlink-scope.c [new file with mode: 0644]
src/core/varlink-scope.h [new file with mode: 0644]
src/core/varlink-unit.c
src/shared/varlink-io.systemd.Unit.c
src/shared/varlink-io.systemd.Unit.h
src/test/test-varlink-idl-unit.c
test/units/TEST-74-AUX-UTILS.varlinkctl.sh

index 0dae1e50cc9debf5151b37fbee0bc2ea8575dcdc..796669814bf351ff9e2152e667d2d9abef7eec87 100644 (file)
@@ -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 (file)
index 0000000..d22a2da
--- /dev/null
@@ -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 (file)
index 0000000..0e941f8
--- /dev/null
@@ -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);
index ca357cf899e8a7729ea15a6483e77f9bbef8ae86..30b912ddf103697f17e7e2caab786e5108df9ac9 100644 (file)
@@ -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(
index 4e8e4f04b7fa2f9a6d7606cbaa0a813a4b3ad870..83a728e2cc68027dfe9ae334ca1bd10ef11cebbc 100644 (file)
@@ -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,
index bc43d7b59fba8f6271ccf3655296987194a8a3e1..54d2dad1c13391d5b4c60f04c95aba7873b5f802 100644 (file)
@@ -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;
index 6c0f834a7a3606ba31e4ae991cbd5d52e0508dee..5e9ce4ce0612270db88904a148fe14855be2e3c9 100644 (file)
@@ -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);
index 1db22b85c118b18148a5eab05158e1c9bb998f6f..900eda1138c25d8bcc61b1ed77454c5311c0a5ac 100755 (executable)
@@ -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