]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: tweak pattern of applying varlink properties for StartTransient
authorMichael Vogt <michael@amutable.com>
Wed, 29 Apr 2026 13:27:54 +0000 (15:27 +0200)
committerMichael Vogt <michael@amutable.com>
Wed, 13 May 2026 17:28:35 +0000 (19:28 +0200)
So far we had the pattern to check first if any property needs setting
and then have a function to set it. The downside is that when we add
a new property two different places need to change (once the `if`
in vl_method_start_transient_unit() and once in the specific helper
to do the actual setting). So instead this commit moves everything to
the helpers and tweaks the code so that we can always call the function.

src/core/varlink-unit.c

index 2e3b1dd1961b8c780235d233c62fc93704611985..85139f068dcb667494b6119525707d08ab198f39 100644 (file)
@@ -703,6 +703,7 @@ static JSON_DISPATCH_ENUM_DEFINE(dispatch_service_type, ServiceType, service_typ
 static JSON_DISPATCH_ENUM_DEFINE(dispatch_job_mode, JobMode, job_mode_from_string);
 
 typedef struct TransientServiceParameters {
+        bool present;
         ServiceType type;
         TransientExecCommandItem *exec_start;
         size_t n_exec_start;
@@ -782,6 +783,7 @@ static int dispatch_transient_service(const char *name, sd_json_variant *variant
         };
 
         StartTransientContextParameters *p = ASSERT_PTR(userdata);
+        p->service.present = true;
         return sd_json_dispatch(variant, service_dispatch, /* flags= */ 0, &p->service);
 }
 
@@ -807,10 +809,26 @@ static int dispatch_transient_context(const char *name, sd_json_variant *variant
         return r;
 }
 
-static int transient_service_apply_properties(Unit *u, TransientServiceParameters *sp) {
+static int transient_unit_apply_properties(Unit *u, StartTransientContextParameters *p) {
+        int r;
+
+        assert(u);
+        assert(p);
+
+        if (p->description) {
+                r = unit_set_description(u, p->description);
+                if (r < 0)
+                        return r;
+                unit_write_settingf(u, UNIT_RUNTIME|UNIT_ESCAPE_SPECIFIERS, "Description", "Description=%s", p->description);
+        }
+
+        return 0;
+}
+
+static int transient_service_apply_properties(Service *s, TransientServiceParameters *sp) {
+        Unit *u = UNIT(ASSERT_PTR(s));
         int r;
 
-        Service *s = ASSERT_PTR(SERVICE(u));
         assert(sp);
 
         if (sp->type >= 0) {
@@ -956,22 +974,18 @@ int vl_method_start_transient_unit(sd_varlink *link, sd_json_variant *parameters
                 return varlink_reply_bus_error(link, r, &bus_error);
 
         /* Apply unit-level properties from context */
-        if (p.context.description) {
-                r = unit_set_description(u, p.context.description);
-                if (r < 0)
-                        return sd_varlink_error(link, VARLINK_ERROR_UNIT_BAD_SETTING, NULL);
-                unit_write_settingf(u, UNIT_RUNTIME|UNIT_ESCAPE_SPECIFIERS, "Description", "Description=%s", p.context.description);
-        }
+        r = transient_unit_apply_properties(u, &p.context);
+        if (r < 0)
+                return sd_varlink_error(link, VARLINK_ERROR_UNIT_BAD_SETTING, NULL);
 
         /* Apply service-specific properties from context.Service */
-        if (p.context.service.type >= 0 || p.context.service.n_exec_start > 0 || p.context.service.remain_after_exit >= 0) {
-                if (t != UNIT_SERVICE)
-                        return sd_varlink_error(link, VARLINK_ERROR_UNIT_TYPE_NOT_SUPPORTED, NULL);
-
-                r = transient_service_apply_properties(u, &p.context.service);
+        Service *s = SERVICE(u);
+        if (s) {
+                r = transient_service_apply_properties(s, &p.context.service);
                 if (r < 0)
                         return sd_varlink_error(link, VARLINK_ERROR_UNIT_BAD_SETTING, NULL);
-        }
+        } else if (p.context.service.present)
+                return sd_varlink_error(link, VARLINK_ERROR_UNIT_TYPE_NOT_SUPPORTED, NULL);
 
         unit_add_to_load_queue(u);
         manager_dispatch_load_queue(manager);