]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: extract exec_context_apply_environment() helper
authorMichael Vogt <michael@amutable.com>
Wed, 29 Apr 2026 13:41:24 +0000 (15:41 +0200)
committerMichael Vogt <michael@amutable.com>
Wed, 13 May 2026 17:28:35 +0000 (19:28 +0200)
Extract a small helper exec_context_apply_environment() from the
dbus-execute.c file so that it can be re-used in the coming varlink
version.

src/core/dbus-execute.c
src/core/execute.c
src/core/execute.h

index 2329762f16b68c8d02e81c2992e6a958ce1cf87d..068b1ebb3d3a4b4ff6e4a02fbce8f9fa5c6c31bd 100644 (file)
@@ -3456,32 +3456,14 @@ int bus_exec_context_set_transient_property(
                 if (r < 0)
                         return r;
 
-                if (strv_length(l) > ENVIRONMENT_ASSIGNMENTS_MAX)
+                r = exec_context_apply_environment(u, c, l, flags);
+                if (r == -E2BIG)
                         return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED,
                                                 "Too many environment assignments.");
-                if (!strv_env_is_valid(l))
+                if (r == -EINVAL)
                         return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment block.");
-
-                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
-                        if (strv_isempty(l)) {
-                                c->environment = strv_free(c->environment);
-                                unit_write_setting(u, flags, name, "Environment=");
-                        } else {
-                                _cleanup_free_ char *joined = NULL;
-                                char **e;
-
-                                joined = unit_concat_strv(l, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
-                                if (!joined)
-                                        return -ENOMEM;
-
-                                e = strv_env_merge(c->environment, l);
-                                if (!e)
-                                        return -ENOMEM;
-
-                                strv_free_and_replace(c->environment, e);
-                                unit_write_settingf(u, flags, name, "Environment=%s", joined);
-                        }
-                }
+                if (r < 0)
+                        return r;
 
                 return 1;
 
index 987093511fc5752da915613354a172b1bc84c170..7f92eba30f53241388e98b6c7e87f7edb5ae206d 100644 (file)
@@ -753,6 +753,41 @@ void exec_context_done(ExecContext *c) {
         c->private_hostname = mfree(c->private_hostname);
 }
 
+int exec_context_apply_environment(
+                Unit *u,
+                ExecContext *c,
+                char **env,
+                UnitWriteFlags flags) {
+
+        assert(u);
+        assert(c);
+
+        if (strv_length(env) > ENVIRONMENT_ASSIGNMENTS_MAX)
+                return -E2BIG;
+        if (!strv_env_is_valid(env))
+                return -EINVAL;
+
+        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+                if (strv_isempty(env)) {
+                        c->environment = strv_free(c->environment);
+                        unit_write_setting(u, flags, "Environment", "Environment=");
+                } else {
+                        _cleanup_free_ char *joined = unit_concat_strv(env, UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C);
+                        if (!joined)
+                                return -ENOMEM;
+
+                        char **e = strv_env_merge(c->environment, env);
+                        if (!e)
+                                return -ENOMEM;
+
+                        strv_free_and_replace(c->environment, e);
+                        unit_write_settingf(u, flags, "Environment", "Environment=%s", joined);
+                }
+        }
+
+        return 0;
+}
+
 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_prefix) {
         assert(c);
 
index 66bb40a6750100abc8d56220e96bee922cca952a..f29f23bc2ef415398b28f4c2dcf5f515d09b2cee 100644 (file)
@@ -540,6 +540,8 @@ void exec_context_init(ExecContext *c);
 void exec_context_done(ExecContext *c);
 void exec_context_dump(const ExecContext *c, FILE* f, const char *prefix);
 
+int exec_context_apply_environment(Unit *u, ExecContext *c, char **env, UnitWriteFlags flags);
+
 int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_prefix);
 int exec_context_destroy_mount_ns_dir(Unit *u);