core: extract exec_context_apply_set_credential() helper

Extract the SetCredential{,Encrypted} logic out of
bus_exec_context_set_transient_property() into a new helper.
No functional changes.

This will be used in the varlink Unit.StartTransient SetCredential
implementation.
This commit is contained in:
Michael Vogt
2026-05-13 19:28:35 +02:00
parent 27fa92aa3c
commit 045d08e417
3 changed files with 58 additions and 26 deletions
+50
View File
@@ -18,11 +18,13 @@
#include "cgroup-setup.h"
#include "coredump-util.h"
#include "cpu-set-util.h"
#include "creds-util.h"
#include "dissect-image.h"
#include "dynamic-user.h"
#include "env-file.h"
#include "env-util.h"
#include "escape.h"
#include "exec-credential.h"
#include "execute.h"
#include "execute-serialize.h"
#include "fd-util.h"
@@ -55,6 +57,7 @@
#include "serialize.h"
#include "set.h"
#include "sort-util.h"
#include "specifier.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
@@ -788,6 +791,53 @@ int exec_context_apply_environment(
return 0;
}
int exec_context_apply_set_credential(
Unit *u,
ExecContext *c,
const char *id,
const void *data,
size_t size,
bool encrypted,
UnitWriteFlags flags,
const char **reterr_message) {
int r;
assert(u);
assert(c);
assert(id);
assert(data || size == 0);
if (!credential_name_valid(id)) {
if (reterr_message)
*reterr_message = "Credential ID is invalid";
return -EINVAL;
}
if (UNIT_WRITE_FLAGS_NOOP(flags))
return 0;
_cleanup_free_ void *copy = memdup(data, size);
if (!copy)
return -ENOMEM;
_cleanup_free_ char *escaped_id = specifier_escape(id);
if (!escaped_id)
return -ENOMEM;
_cleanup_free_ char *escaped_value = cescape_length(data, size);
if (!escaped_value)
return -ENOMEM;
r = exec_context_put_set_credential(c, id, TAKE_PTR(copy), size, encrypted);
if (r < 0)
return r;
const char *name = encrypted ? "SetCredentialEncrypted" : "SetCredential";
unit_write_settingf(u, flags, name, "%s=%s:%s", name, escaped_id, escaped_value);
return 0;
}
int exec_context_destroy_runtime_directory(const ExecContext *c, const char *runtime_prefix) {
assert(c);