core: extract exec_context_apply_environment() helper

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.
This commit is contained in:
Michael Vogt
2026-05-13 19:28:35 +02:00
parent a2aa3a7450
commit ed6b2b9c30
3 changed files with 42 additions and 23 deletions
+5 -23
View 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;
+35
View 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);
+2
View 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);