]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-lookup: add runtime_directory for resolving $RUNTIME_DIRECTORY
authorSam Leonard <sam.leonard@codethink.co.uk>
Tue, 23 Jan 2024 13:39:32 +0000 (13:39 +0000)
committerSam Leonard <sam.leonard@codethink.co.uk>
Fri, 9 Feb 2024 11:43:18 +0000 (11:43 +0000)
src/basic/path-lookup.c
src/basic/path-lookup.h

index 068054512bcb1cc756af3cc8673ae429720a851c..1b31a4e23a52c463fdb840499486f8d9a5afda9b 100644 (file)
@@ -91,6 +91,44 @@ int xdg_user_data_dir(char **ret, const char *suffix) {
         return 1;
 }
 
+int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) {
+        _cleanup_free_ char *d = NULL;
+        int r;
+
+        assert(ret);
+        assert(suffix);
+        assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER, RUNTIME_SCOPE_GLOBAL));
+
+        /* Accept $RUNTIME_DIRECTORY as authoritative
+         * If its missing apply the suffix to /run or $XDG_RUNTIME_DIR
+         * if we are in a user runtime scope.
+         *
+         * Return value indicates whether the suffix was applied or not */
+
+        const char *e = secure_getenv("RUNTIME_DIRECTORY");
+        if (e) {
+                d = strdup(e);
+                if (!d)
+                        return -ENOMEM;
+
+                *ret = TAKE_PTR(d);
+                return false;
+        }
+
+        if (scope == RUNTIME_SCOPE_USER) {
+                r = xdg_user_runtime_dir(&d, suffix);
+                if (r < 0)
+                        return r;
+        } else {
+                d = path_join("/run", suffix);
+                if (!d)
+                        return -ENOMEM;
+        }
+
+        *ret = TAKE_PTR(d);
+        return true;
+}
+
 static const char* const user_data_unit_paths[] = {
         "/usr/local/lib/systemd/user",
         "/usr/local/share/systemd/user",
index 1601787064ea294a569d76e32072257c7b9018c9..11db264c01a297aaa9776329f433bdd612d89d2d 100644 (file)
@@ -59,6 +59,7 @@ int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs);
 int xdg_user_runtime_dir(char **ret, const char *suffix);
 int xdg_user_config_dir(char **ret, const char *suffix);
 int xdg_user_data_dir(char **ret, const char *suffix);
+int runtime_directory(char **ret, RuntimeScope scope, const char *suffix);
 
 bool path_is_user_data_dir(const char *path);
 bool path_is_user_config_dir(const char *path);