]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
build-path: allow overriding of all callout binary paths via an env var
authorLennart Poettering <lennart@poettering.net>
Tue, 20 Feb 2024 10:57:39 +0000 (11:57 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 21 Feb 2024 08:25:46 +0000 (09:25 +0100)
src/basic/build-path.c

index d81ed561dc94e3621c88fb22082d247d5cd75ac5..0c43afaefdf502723c62b348bef03a8a714a19e2 100644 (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);