From: Luca Boccassi Date: Sat, 20 Dec 2025 16:54:45 +0000 (+0000) Subject: core: add SetProperties varlink method X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e1c4de235908dfe507fbbddb06ad49b53ccb86b;p=thirdparty%2Fsystemd.git core: add SetProperties varlink method Initial support for 'Markers' only --- diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 2c61ddeb80c..c2b562458be 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -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); diff --git a/src/core/unit.c b/src/core/unit.c index e172a9e38c0..0f6ea877561 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -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; +} diff --git a/src/core/unit.h b/src/core/unit.h index 55a51a6f0e0..95959cfcea4 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -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, ...) \ diff --git a/src/core/varlink-unit.c b/src/core/varlink-unit.c index 71430ca5d38..790d0df2dfb 100644 --- a/src/core/varlink-unit.c +++ b/src/core/varlink-unit.c @@ -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); +} diff --git a/src/core/varlink-unit.h b/src/core/varlink-unit.h index 73de837940f..8f58d7ce103 100644 --- a/src/core/varlink-unit.h +++ b/src/core/varlink-unit.h @@ -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); diff --git a/src/core/varlink.c b/src/core/varlink.c index b5b00cdf740..b7734e2b03c 100644 --- a/src/core/varlink.c +++ b/src/core/varlink.c @@ -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) diff --git a/src/shared/varlink-io.systemd.Unit.c b/src/shared/varlink-io.systemd.Unit.c index b9128e78dfe..dba9ad9b86b 100644 --- a/src/shared/varlink-io.systemd.Unit.c +++ b/src/shared/varlink-io.systemd.Unit.c @@ -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"), diff --git a/test/units/TEST-26-SYSTEMCTL.sh b/test/units/TEST-26-SYSTEMCTL.sh index 793ecf3af45..2b8c6fa2308 100755 --- a/test/units/TEST-26-SYSTEMCTL.sh +++ b/test/units/TEST-26-SYSTEMCTL.sh @@ -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" diff --git a/test/units/TEST-74-AUX-UTILS.varlinkctl.sh b/test/units/TEST-74-AUX-UTILS.varlinkctl.sh index c22b5e7030e..9d3e6a0bbc6 100755 --- a/test/units/TEST-74-AUX-UTILS.varlinkctl.sh +++ b/test/units/TEST-74-AUX-UTILS.varlinkctl.sh @@ -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)"