]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: add SetProperties varlink method 40356/head
authorLuca Boccassi <luca.boccassi@gmail.com>
Sat, 20 Dec 2025 16:54:45 +0000 (16:54 +0000)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 13 Feb 2026 19:16:13 +0000 (19:16 +0000)
Initial support for 'Markers' only

src/core/dbus-unit.c
src/core/unit.c
src/core/unit.h
src/core/varlink-unit.c
src/core/varlink-unit.h
src/core/varlink.c
src/shared/varlink-io.systemd.Unit.c
test/units/TEST-26-SYSTEMCTL.sh
test/units/TEST-74-AUX-UTILS.varlinkctl.sh

index 2c61ddeb80ce0d222dd44cfc944f1a11cb36ba83..c2b562458bef062bc39ba3ae8cdbd672ff06ad1a 100644 (file)
@@ -2121,7 +2121,6 @@ static int bus_unit_set_live_property(
 
                 for (;;) {
                         const char *word;
-                        bool b;
 
                         r = sd_bus_message_read(message, "s", &word);
                         if (r < 0)
@@ -2129,22 +2128,14 @@ static int bus_unit_set_live_property(
                         if (r == 0)
                                 break;
 
-                        if (IN_SET(word[0], '+', '-')) {
-                                b = word[0] == '+';
-                                word++;
-                                some_plus_minus = true;
-                        } else {
-                                b = true;
-                                some_absolute = true;
-                        }
-
-                        UnitMarker m = unit_marker_from_string(word);
-                        if (m < 0)
+                        r = parse_unit_marker(word, &settings, &mask);
+                        if (r < 0)
                                 return sd_bus_error_setf(reterr_error, BUS_ERROR_BAD_UNIT_SETTING,
                                                          "Unknown marker \"%s\".", word);
-
-                        SET_FLAG(settings, 1u << m, b);
-                        SET_FLAG(mask, 1u << m, true);
+                        if (r > 0)
+                                some_plus_minus = true;
+                        else
+                                some_absolute = true;
                 }
 
                 r = sd_bus_message_exit_container(message);
index e172a9e38c087fc6396b55d501ea019b7fd68190..0f6ea87756157a2cb86a12b5839ef0115209d75d 100644 (file)
@@ -7058,3 +7058,26 @@ int unit_queue_job_check_and_mangle_type(
 
         return 0;
 }
+
+int parse_unit_marker(const char *marker, unsigned *settings, unsigned *mask) {
+        bool some_plus_minus = false, b = true;
+
+        assert(marker);
+        assert(settings);
+        assert(mask);
+
+        if (IN_SET(marker[0], '+', '-')) {
+                b = marker[0] == '+';
+                marker++;
+                some_plus_minus = true;
+        }
+
+        UnitMarker m = unit_marker_from_string(marker);
+        if (m < 0)
+                return -EINVAL;
+
+        SET_FLAG(*settings, 1u << m, b);
+        SET_FLAG(*mask, 1u << m, true);
+
+        return some_plus_minus;
+}
index 55a51a6f0e0569543597a3e851c4347a7406428a..95959cfcea45cc45630aedd5c4b8d43811fec152 100644 (file)
@@ -1094,6 +1094,8 @@ DECLARE_STRING_TABLE_LOOKUP(oom_policy, OOMPolicy);
 
 int unit_queue_job_check_and_mangle_type(Unit *u, JobType *type, bool reload_if_possible, sd_bus_error *reterr_error);
 
+int parse_unit_marker(const char *marker, unsigned *settings, unsigned *mask);
+
 /* Macros which append UNIT= or USER_UNIT= to the message */
 
 #define log_unit_full_errno_zerook(unit, level, error, ...)             \
index 71430ca5d38916b6754bd9d002ba7d408514b545..790d0df2dfb3407fae75841b33edc024f6d4aaf4 100644 (file)
@@ -568,3 +568,107 @@ int varlink_error_no_such_unit(sd_varlink *v, const char *name) {
                         VARLINK_ERROR_UNIT_NO_SUCH_UNIT,
                         JSON_BUILD_PAIR_STRING_NON_EMPTY("parameter", name));
 }
+
+typedef struct UnitSetPropertiesParameters {
+        const char *unsupported_property; /* For error reporting */
+        const char *name;
+        bool runtime;
+
+        bool markers_found;
+        unsigned markers, markers_mask;
+} UnitSetPropertiesParameters;
+
+static int parse_unit_markers(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
+        UnitSetPropertiesParameters *p = ASSERT_PTR(userdata);
+        bool some_plus_minus = false, some_absolute = false;
+        unsigned settings = 0, mask = 0;
+        sd_json_variant *e;
+        int r;
+
+        assert(variant);
+
+        JSON_VARIANT_ARRAY_FOREACH(e, variant) {
+                if (!sd_json_variant_is_string(e))
+                        return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Marker is not an array of strings.");
+
+                const char *word = sd_json_variant_string(e);
+
+                r = parse_unit_marker(word, &settings, &mask);
+                if (r < 0)
+                        return json_log(variant, flags, r, "Failed to parse marker '%s'.", word);
+                if (r > 0)
+                        some_plus_minus = true;
+                else
+                        some_absolute = true;
+        }
+
+        if (some_plus_minus && some_absolute)
+                return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Absolute and non-absolute markers in the same setting.");
+
+        if (some_absolute || sd_json_variant_elements(variant) == 0)
+                mask = UINT_MAX;
+
+        p->markers = settings;
+        p->markers_mask = mask;
+        p->markers_found = true;
+
+        return 0;
+}
+
+static int unit_dispatch_properties(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
+        static const sd_json_dispatch_field dispatch_table[] = {
+                { "Markers", SD_JSON_VARIANT_ARRAY, parse_unit_markers, 0, 0 },
+                {}
+        };
+        UnitSetPropertiesParameters *p = ASSERT_PTR(userdata);
+        const char *bad_field = NULL;
+        int r;
+
+        r = sd_json_dispatch_full(variant, dispatch_table, /* bad= */ NULL, flags, userdata, &bad_field);
+        if (r == -EADDRNOTAVAIL && !isempty(bad_field))
+                /* When properties contains a valid field, but that we don't currently support, make sure to
+                 * return the offending property, rather than generic invalid argument. */
+                p->unsupported_property = bad_field;
+        return r;
+}
+
+int vl_method_set_unit_properties(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
+        static const sd_json_dispatch_field dispatch_table[] = {
+                { "name",       SD_JSON_VARIANT_STRING,  json_dispatch_const_unit_name, offsetof(UnitSetPropertiesParameters, name),    SD_JSON_MANDATORY },
+                { "runtime",    SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool,      offsetof(UnitSetPropertiesParameters, runtime), SD_JSON_MANDATORY },
+                { "properties", SD_JSON_VARIANT_OBJECT,  unit_dispatch_properties,      0,                                              SD_JSON_MANDATORY },
+                {}
+        };
+
+        UnitSetPropertiesParameters p = {};
+        Manager *manager = ASSERT_PTR(userdata);
+        const char *bad_field = NULL;
+        Unit *unit;
+        int r;
+
+        assert(link);
+        assert(parameters);
+
+        r = sd_json_dispatch_full(parameters, dispatch_table, /* bad= */ NULL, /* flags= */ 0, &p, &bad_field);
+        if (r < 0) {
+                /* When properties contains a valid field, but that we don't currently support, make sure to
+                 * return a specific error, rather than generic invalid argument. */
+                if (streq_ptr(bad_field, "properties") && r == -EADDRNOTAVAIL)
+                        return sd_varlink_errorbo(
+                                link,
+                                "io.systemd.Unit.PropertyNotSupported",
+                                SD_JSON_BUILD_PAIR_CONDITION(!!p.unsupported_property, "property", SD_JSON_BUILD_STRING(p.unsupported_property)));
+                if (bad_field)
+                        return sd_varlink_error_invalid_parameter_name(link, bad_field);
+                return r;
+        }
+
+        r = load_unit_and_check(link, manager, p.name, &unit);
+        if (r < 0)
+                return r;
+
+        if (p.markers_found)
+                unit->markers = p.markers | (unit->markers & ~p.markers_mask);
+
+        return sd_varlink_reply(link, NULL);
+}
index 73de837940fa5340e3ad7b9b4b87bdf857a6ce45..8f58d7ce1037824838e29b2db6fb34ac8582f503 100644 (file)
@@ -17,4 +17,6 @@ int varlink_unit_queue_job_one(
                 uint32_t *ret_job_id,
                 sd_bus_error *reterr_bus_error);
 
+int vl_method_set_unit_properties(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata);
+
 int varlink_error_no_such_unit(sd_varlink *v, const char *name);
index b5b00cdf74044549b809792a2b71619dcf58b5bb..b7734e2b03c8837fe92990fe5d7fe460ae5c8f27 100644 (file)
@@ -390,6 +390,7 @@ int manager_setup_varlink_server(Manager *m) {
                         "io.systemd.Manager.Reload", vl_method_reload_manager,
                         "io.systemd.Manager.EnqueueMarkedJobs", vl_method_enqueue_marked_jobs_manager,
                         "io.systemd.Unit.List", vl_method_list_units,
+                        "io.systemd.Unit.SetProperties", vl_method_set_unit_properties,
                         "io.systemd.service.Ping", varlink_method_ping,
                         "io.systemd.service.GetEnvironment", varlink_method_get_environment);
         if (r < 0)
index b9128e78dfe6914023a460c6acf2e6b452e00b32..dba9ad9b86b8165ace5abf26f94a2b612dfd2178 100644 (file)
@@ -949,13 +949,13 @@ static SD_VARLINK_DEFINE_STRUCT_TYPE(
                 SD_VARLINK_FIELD_COMMENT("If not empty, the field contains the name of another unit that this unit follows in state"),
                 SD_VARLINK_DEFINE_FIELD(Following, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the configuration file of this unit has been loaded"),
-                SD_VARLINK_DEFINE_FIELD(LoadState, SD_VARLINK_STRING, 0),
+                SD_VARLINK_DEFINE_FIELD(LoadState, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the unit is currently active or not"),
-                SD_VARLINK_DEFINE_FIELD(ActiveState, SD_VARLINK_STRING, 0),
+                SD_VARLINK_DEFINE_FIELD(ActiveState, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the unit is currently frozen or not"),
-                SD_VARLINK_DEFINE_FIELD(FreezerState, SD_VARLINK_STRING, 0),
+                SD_VARLINK_DEFINE_FIELD(FreezerState, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflect more fine-grained state that is unit-type-specific"),
-                SD_VARLINK_DEFINE_FIELD(SubState, SD_VARLINK_STRING, 0),
+                SD_VARLINK_DEFINE_FIELD(SubState, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects the install state of the unit file"),
                 SD_VARLINK_DEFINE_FIELD(UnitFileState, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Timestamp when the firmware first began execution"),
@@ -969,27 +969,27 @@ static SD_VARLINK_DEFINE_STRUCT_TYPE(
                 SD_VARLINK_FIELD_COMMENT("Timestamp when the unit exited inactive state"),
                 SD_VARLINK_DEFINE_FIELD_BY_TYPE(InactiveExitTimestamp, Timestamp, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the unit can be started or not"),
-                SD_VARLINK_DEFINE_FIELD(CanStart, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(CanStart, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the unit can be stopped or not"),
-                SD_VARLINK_DEFINE_FIELD(CanStop, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(CanStop, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the unit can be reloaded or not"),
-                SD_VARLINK_DEFINE_FIELD(CanReload, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(CanReload, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Reflects whether the unit may be started in isolation mode"),
-                SD_VARLINK_DEFINE_FIELD(CanIsolate, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(CanIsolate, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Returns which unit resources can be cleaned up"),
                 SD_VARLINK_DEFINE_FIELD(CanClean, SD_VARLINK_STRING, SD_VARLINK_ARRAY|SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Whether the unit supports the freeze operation"),
-                SD_VARLINK_DEFINE_FIELD(CanFreeze, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(CanFreeze, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Whether the unit supports live mounting"),
-                SD_VARLINK_DEFINE_FIELD(CanLiveMount, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(CanLiveMount, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("The job ID of the job currently scheduled or being executed for this unit, if there is any."),
                 SD_VARLINK_DEFINE_FIELD(JobId, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Whether the configuration file this unit is loaded from (i.e. FragmentPath or SourcePath) has changed since the configuration was read and hence whether a configuration reload is recommended"),
-                SD_VARLINK_DEFINE_FIELD(NeedDaemonReload, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(NeedDaemonReload, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Condition result of the last time the configured conditions of this unit were checked"),
-                SD_VARLINK_DEFINE_FIELD(ConditionResult, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(ConditionResult, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Assert result of the last time the configured asserts of this unit were checked"),
-                SD_VARLINK_DEFINE_FIELD(AssertResult, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_DEFINE_FIELD(AssertResult, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("The last time the configured conditions of the unit have been checked"),
                 SD_VARLINK_DEFINE_FIELD_BY_TYPE(ConditionTimestamp, Timestamp, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("The last time the configured asserts of the unit have been checked"),
@@ -1027,12 +1027,26 @@ static SD_VARLINK_DEFINE_ERROR(OnlyByDependency);
 static SD_VARLINK_DEFINE_ERROR(DBusShuttingDown);
 static SD_VARLINK_DEFINE_ERROR(UnitMasked);
 static SD_VARLINK_DEFINE_ERROR(UnitError);
+static SD_VARLINK_DEFINE_ERROR(
+                PropertyNotSupported,
+                SD_VARLINK_DEFINE_FIELD(property, SD_VARLINK_STRING, SD_VARLINK_NULLABLE));
+
+static SD_VARLINK_DEFINE_METHOD(
+                SetProperties,
+                SD_VARLINK_FIELD_COMMENT("The name of the unit to operate on."),
+                SD_VARLINK_DEFINE_INPUT(name, SD_VARLINK_STRING, 0),
+                SD_VARLINK_FIELD_COMMENT("Whether to apply the change ephemerally or persistently."),
+                SD_VARLINK_DEFINE_INPUT(runtime, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_FIELD_COMMENT("The runtime properties to set."),
+                SD_VARLINK_DEFINE_INPUT_BY_TYPE(properties, UnitRuntime, 0));
 
 SD_VARLINK_DEFINE_INTERFACE(
                 io_systemd_Unit,
                 "io.systemd.Unit",
                 SD_VARLINK_SYMBOL_COMMENT("List units"),
                 &vl_method_List,
+                SD_VARLINK_SYMBOL_COMMENT("Set unit properties"),
+                &vl_method_SetProperties,
                 &vl_type_RateLimit,
                 SD_VARLINK_SYMBOL_COMMENT("An object to represent a unit's conditions"),
                 &vl_type_Condition,
@@ -1097,6 +1111,8 @@ SD_VARLINK_DEFINE_INTERFACE(
                 &vl_error_UnitMasked,
                 SD_VARLINK_SYMBOL_COMMENT("Unit is in a fatal error state"),
                 &vl_error_UnitError,
+                SD_VARLINK_SYMBOL_COMMENT("Changing this property via SetProperties() is not supported"),
+                &vl_error_PropertyNotSupported,
                 SD_VARLINK_SYMBOL_COMMENT("Job for the unit may only be enqueued by dependencies"),
                 &vl_error_OnlyByDependency,
                 SD_VARLINK_SYMBOL_COMMENT("A unit that requires D-Bus cannot be started as D-Bus is shutting down"),
index 793ecf3af45e987c84732a5cabdc898f0f0b59ef..2b8c6fa2308ce836d0833f755361b7503ca41857 100755 (executable)
@@ -378,7 +378,7 @@ systemctl reload-or-restart --marked
 
 # again, but with varlinkctl instead
 systemctl restart "$UNIT_NAME"
-systemctl set-property "$UNIT_NAME" Markers=needs-restart
+varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.SetProperties "{\"runtime\": true, \"name\": \"$UNIT_NAME\", \"properties\": {\"Markers\": [\"needs-restart\"]}}"
 systemctl show -P Markers "$UNIT_NAME" | grep needs-restart
 varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Manager.EnqueueMarkedJobs '{}'
 timeout 30 bash -c "until systemctl list-jobs $UNIT_NAME | grep \"No jobs\" 2>/dev/null; do sleep 1; done"
index c22b5e7030ea0af3b3339072d92e22d636fe783d..9d3e6a0bbc603168e048eb5954ef737cc1d60aaf 100755 (executable)
@@ -209,6 +209,10 @@ varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List '{"name": "
 (! varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List '{"name": "non-existent.service"}')
 (! varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List '{"pid": {"pid": -1}}' )
 (! varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List '{"name": "multi-user.target", "pid": {"pid": 1}}')
+set +o pipefail
+varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.SetProperties '{"runtime": true, "name": "non-existent.service", "properties": {"Markers": ["needs-restart"]}}' |& grep "io.systemd.Unit.NoSuchUnit"
+varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.SetProperties '{"runtime": true, "name": "systemd-journald.service", "properties": {"LoadState": "foobar"}}' |& grep "io.systemd.Unit.PropertyNotSupported"
+set -o pipefail
 
 varlinkctl call /run/systemd/io.systemd.Manager io.systemd.Unit.List '{"cgroup": "/init.scope"}'
 invocation_id="$(systemctl show -P InvocationID systemd-journald.service)"