core: add SetProperties varlink method (#40356)

Start with Markers as a property, more can be added in the future as
needed
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-02-17 10:57:09 +01:00
committed by GitHub
9 changed files with 215 additions and 32 deletions
+6 -15
View 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);
+23
View 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;
}
+2
View 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, ...) \
+141 -3
View File
@@ -361,6 +361,40 @@ static int lookup_unit_by_pidref(sd_varlink *link, Manager *manager, PidRef *pid
return 0;
}
static int load_unit_and_check(sd_varlink *link, Manager *manager, const char *name, Unit **ret_unit) {
Unit *unit;
int r;
assert(link);
assert(manager);
assert(name);
assert(ret_unit);
r = manager_load_unit(manager, name, /* path= */ NULL, /* e= */ NULL, &unit);
if (r < 0)
return r;
/* manager_load_unit() will create an object regardless of whether the unit actually exists, so
* check the state and refuse if it's not in a good state. */
if (IN_SET(unit->load_state, UNIT_NOT_FOUND, UNIT_STUB, UNIT_MERGED))
return sd_varlink_error(link, "io.systemd.Unit.NoSuchUnit", NULL);
if (unit->load_state == UNIT_BAD_SETTING)
return sd_varlink_error(link, "io.systemd.Unit.UnitError", NULL);
if (unit->load_state == UNIT_ERROR)
return sd_varlink_errorbo(
link,
SD_VARLINK_ERROR_SYSTEM,
SD_JSON_BUILD_PAIR_STRING("origin", "linux"),
SD_JSON_BUILD_PAIR_INTEGER("errno", unit->load_error),
JSON_BUILD_PAIR_STRING_NON_EMPTY("errnoName", "io.systemd.Unit.UnitError"));
if (unit->load_state == UNIT_MASKED)
return sd_varlink_error(link, "io.systemd.Unit.UnitMasked", NULL);
assert(UNIT_IS_LOAD_COMPLETE(unit->load_state));
*ret_unit = unit;
return 0;
}
typedef struct UnitLookupParameters {
const char *name, *cgroup;
PidRef pidref;
@@ -400,9 +434,9 @@ static int lookup_unit_by_parameters(
assert(ret);
if (p->name) {
unit = manager_get_unit(manager, p->name);
if (!unit)
return varlink_error_no_such_unit(link, "name");
r = load_unit_and_check(link, manager, p->name, &unit);
if (r < 0)
return r;
}
if (pidref_is_set_or_automatic(&p->pidref)) {
@@ -534,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);
}
+2
View 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);
+1
View File
@@ -391,6 +391,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)
+35 -13
View File
@@ -951,13 +951,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"),
@@ -971,27 +971,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,28 @@ static SD_VARLINK_DEFINE_ERROR(
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,
@@ -1093,6 +1109,12 @@ SD_VARLINK_DEFINE_INTERFACE(
/* Errors */
SD_VARLINK_SYMBOL_COMMENT("No matching unit found"),
&vl_error_NoSuchUnit,
SD_VARLINK_SYMBOL_COMMENT("The unit is masked"),
&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"),
+1 -1
View File
@@ -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"
@@ -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)"