build-path: allow overriding of all callout binary paths via an env var

This commit is contained in:
Lennart Poettering
2024-02-20 11:57:39 +01:00
parent 91d149cfb4
commit 950ca1788e

View File

@@ -184,6 +184,30 @@ static int find_build_dir_binary(const char *fn, char **ret) {
return 0;
}
static int find_environment_binary(const char *fn, const char **ret) {
/* If a path such as /usr/lib/systemd/systemd-foobar is specified, then this will check for an
* environment variable SYSTEMD_FOOBAR_PATH and return it if set. */
_cleanup_free_ char *s = strdup(fn);
if (!s)
return -ENOMEM;
ascii_strupper(s);
string_replace_char(s, '-', '_');
if (!strextend(&s, "_PATH"))
return -ENOMEM;
const char *e;
e = secure_getenv(s);
if (!e)
return -ENXIO;
*ret = e;
return 0;
}
int invoke_callout_binary(const char *path, char *const argv[]) {
int r;
@@ -198,6 +222,13 @@ int invoke_callout_binary(const char *path, char *const argv[]) {
if (r == O_DIRECTORY) /* Uh? */
return -EISDIR;
const char *e;
if (find_environment_binary(fn, &e) >= 0) {
/* If there's an explicit environment variable set for this binary, prefer it */
execv(e, argv);
return -errno; /* The environment variable counts, let's fail otherwise */
}
_cleanup_free_ char *np = NULL;
if (find_build_dir_binary(fn, &np) >= 0)
execv(np, argv);